Make REST API on Meteor+React without other packages - json

For example, when the path is
/json/users/4
meteor app must return json something like
{
id: 4,
name: 'Alex'
}
I'm using reactrouter:react-router for client routing. I know about reactrouter:react-router-ssr, but how to use it to response raw json? And make it not conflicting with existing client routing?

I found the answer. Meteor's default Webapp package will help (doc):
WebApp.connectHandlers.use("/hello", function(req, res, next) {
res.writeHead(200);
res.end("Hello world from: " + Meteor.release);
});
I put this in server folder. Other routes will be rendered as they was.
So, there is more useful example (es6):
WebApp.connectHandlers.use("/payme", function(req, res, next) {
res.writeHead(200, {'Content-Type': 'application/json'});
if (req.method === 'POST') {
req.on('data', (chunk) => {
const body = chunk.toString();
if (body.length < 1e6) {
const params = body.split('&').reduce((result, item) => {
const [key, val] = item.split('=');
//do it for utf-8 values (I use it for cyrillic strings)
result[key] = unescape(decodeURI(val)).replace(/\+/g, ' ');
return result;
}, {}); //post method params
//do something and get resulting json
res.end(JSON.stringify(result));
} else
res.end(JSON.stringify({error: 'too big query'}));
});
} else
res.end(JSON.stringify({error: 'isnt post req'}));
});
req.query can be used to get GET params.

Related

how to make a post request inside async function?

At the end of the waterfall-dialog in "summary" (i.e., the last if statement) i want to automatically make a post request without making an API call in Postman, is eventListener the way? How to include it?
async summaryStep(step) {
if (step.result) {
// Get the current profile object from user state.
const userProfile = await this.userProfile.get(step.context, new UserProfile());
userProfile.name = step.values.name;
//the same for other step values(email, doctor, date)
let msg = `you want a date with dr. ${userProfile.doctor} , and your name is ${userProfile.name}.`;
if (userProfile.date !== -1) {
msg += `you have an appointment the: ${userProfile.date}.`;
}
await step.context.sendActivity(msg);
let msg1 = `"${userProfile.date}"`;
if (msg1) {
let z = JSON.stringify(userProfile.name);
//and also the other rows to go in the database(email, doctor, date)
var name = JSON.parse(z);
//and also the other rows to go in the database(email, doctor, date)
//this actually works but only if i use postman
var urlencoded = bodyparser.urlencoded({ extended: false });
app.post('/id', urlencoded, (req, res) => {
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
mysqlConnection.query("INSERT INTO users(name, email, doctor, date) VALUES('" + userProfile.name + "','" + userProfile.password + "','" + userProfile.doctor + "','" + userProfile.date + "')", function (err, result, rows) {
if (err) throw err;
console.log("Yeah! record inserted");
console.log(name);
res.send(result);
});
});
const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`Listening on port ${port}..`));
}
} else {
await step.context.sendActivity('Thanks. Your profile will not be kept. Push enter to return Menu');
}
return await step.prompt(CONFIRM_PROMPT3, `is that true? ${step.result}`, ['yes', 'no']);
// this if statement should "fire" the post request...
if (step.result == 'yes') {
return await step.context.sendActivity(`we will contact you soon ${userProfile.password}.`);
}
return await step.endDialog();
}
Per my understanding , you want to know how to call an POST API from Azure bot async function. Pls try the code below in your async summaryStep function to send the post request based on your requirement.
var rp = require('request-promise');
var options = {
method: 'POST',
uri: 'http://localhost:8080/id',
body: {
fieldCount:0,
affectedRows:1,
//your other body content here...
},
json: true,
headers: {
'content-type': 'application/json' //you can append other headers here
}
};
await rp(options)
.then(function (body) {
console.log(body)
})
.catch(function (err) {
console.log(err)
});
}
Hope it helps .
A
nd if there is any further concerns or misunderstand , pls feel free to let me know.
The answer is to move your app.post API endpoint to your index.js file where your bot is already running on a server. Simply spin up a new "server" and "port" making the endpoint available. Then, in your summaryStep (axiosStep in my example), make your API call using Axios, request-promise, or what have you, to post your data. When the API is hit, the data will be passed in and processed.
In the code below, when the API is hit the passed in data is used in a sendActivity posted back to the bot. In your case, your passed in data would be used for the database call in which you could use the returned response in the sendActivity.
Your code would look something like the following. Please note, the post actions are simplified for the sake of the example. You would need to update the post actions to make your mySql queries. This sample also makes use of restify for the server (standard for Bot Framework bots) and uses the same port as the bot, but this can easily be updated to use Express and/or another port.
Hope of help!
index.js
[...]
const conversationReferences = {};
const bodyParser = require('body-parser');
server.post('/id', async (req, res) => {
const { conversationID, data, name } = req.body;
const conversationReference = conversationReferences[ conversationID ];
await adapter.continueConversation(conversationReference, async turnContext => {
var reply = `${ data }. Thanks, ${ name }`;
await turnContext.sendActivity(reply);
});
res.writeHead(200);
res.end();
});
mainDialog.js
async axiosStep ( stepContext ) {
const conversationID = stepContext.context.activity.conversation.id;
try {
const response = await axios.post(`http://localhost:3978/id`, {
data: "Yeah! Record inserted",
name: "Steve",
conversationID: conversationID
})
console.log(response);
} catch (error) {
console.log(error);
}
return stepContext.next();
}

How to dynamically fetch JSON based on param in express

I'm new to node and express but trying to dynamically fetch JSON based on the user's language settings. I need to figure out how to serve up the params:
I was thinking to try and set the "lang" param to its own variable and return that, but I am reading on SO that this is not best practice: Use variable's value as variable in javascript
var express = require("express");
var english = require('./Data/english.json');
var spanish = require('./Data/spanish.json');
var app = express();
app.use(function (req, res, next) {
console.log('inside of app.use');
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-
With, Content-Type, Accept");
next();
});
app.get("/:lang", function (req, res, next) {
const lang = req.params.lang;
console.log(lang)
res.send(lang);
});
app.listen(5000, () => console.log('Listening on port 5000!'))
I would like to be able to dynamically return the appropriate json file ! Any thoughts much appreciated.
TL;DR
Use the fs module to read you JSON files
Call JSON.parse() to parse the raw JSON
Choose the file according to var lang = req.params.lang using some kind of logic (for example, 'spanish' -> './data/spanish.json')
Code
const fs = require('fs');
// Supported languages
const supportedLanguages = ['english', 'spanish'];
app.get("/:lang", function (req, res, next) {
const lang = req.params.lang;
if (supportedLanguages.indexOf(lang) === -1) {
res.status(400).send('Language not supported');
} else {
fs.readFile(`./Data/${lang}.json`, (err, data) => {
// If error send a 500 status
if (err) res.status(500).send(err);
// Else parse the JSON file and send it
else res.send(JSON.parse(data));
});
}
});
It's simple to return json data with express.
Just use res.json({key: value , key2: value2}) instead of res.send
In your case, you can
const language = req.params.lang;
res.json({lang: lang})
you will receive a JSON object instead of text

Node.JS: How to scrape a json page for specific data

I would like to scrape this page: calendar events
for specific data, like formattedDate and description. How do I go about that in a module in Node.JS. I am having a hard time understanding the process in Node.JS.
Any help would go a long way, thanks in advance.
it's pretty simple, you can import the request module and use it. For example, see code below.
const request = require("request");
request("MY_URL", (error, response, body) => {
console.log('body:', body);
});
Also, you can try this here, on Repl.it
First of all, you need to parse your JSON, this allows you to access fields from received json.
const data = JSON.parse(body);
Now, if you want to access some information about an event you need to loop events and access what you need, something like:
const events = data.bwEventList.events;
events.map((data, index) => console.log(data.calendar))
Final code also on Repl.it
from nodeJS docs here
const http = require('http');
http.get('http://umd.bwcs-hosting.com/feeder/main/eventsFeed.do?f=y&sort=dtstart.utc:asc&fexpr=(categories.href!=%22/public/.bedework/categories/sys/Ongoing%22%20and%20categories.href!=%22/public/.bedework/categories/Campus%20Bulletin%20Board%22)%20and%20(entity_type=%22event%22%7Centity_type=%22todo%22)&skinName=list-json&count=30', (res) => {
const { statusCode } = res;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 200) {
error = new Error('Request Failed.\n' +
`Status Code: ${statusCode}`);
}
if (error) {
console.error(error.message);
// consume response data to free up memory
res.resume();
return;
}
res.setEncoding('utf8');
let rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
try {
const parsedData = JSON.parse(rawData);
console.log(parsedData["bwEventList"]["resultSize"]);
} catch (e) {
console.error(e.message);
}
});
}).on('error', (e) => {
console.error(`Got error: ${e.message}`);
});
see console.log(parsedData["bwEventList"]["resultSize"]);
slice parsedData as an array until you get what you want

400/500 error handler in Express - sending JSON vs HTML

We have this error handler in Express:
app.use(function (err, req, res, next) {
res.status(err.status || 500);
const stck = String(err.stack || err).split('\n').filter(function (s) {
return !String(s).match(/\/node_modules\// && String(s).match(/\//));
});
const joined = stck.join('\n');
console.error(joined);
const isProd = process.env.NODE_ENV === 'production';
const message = res.locals.message = (err.message || err);
const shortStackTrace = res.locals.shortStackTrace = isProd ? '' : joined;
const fullStackTrace = res.locals.fullStackTrace = isProd ? '': (err.stack || err);
if (req.headers['Content-Type'] === 'application/json') {
res.json({
message: message,
shortStackTrace: shortStackTrace,
fullStackTrace: fullStackTrace
});
}
else {
//locals for template have already been set
res.render('error');
}
});
My question is - we want to send back either JSON or HTML depending on the type of request. I assume looking at Content-Type header is the best way to do this. Is there any other way I should be checking?
Isn't the Content-Type header sometimes called 'content-type' (lowercase)?
I prefer using the following (this is for a 404 error but that's not important):
if (req.accepts('html')) {
// Respond with html page.
fs.readFile('404.html', 'utf-8', function(err, page) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.write(page);
res.end();
});
} else {
if (req.accepts('json')) {
// Respond with json.
res.status(404).send({ error: 'Not found' });
} else {
// Default to plain-text. send()
res.status(404).type('txt').send('Not found');
}
}
Basically you can use the req object's accepts method to find out what you should send as a response.

Parsing JSON in Express without BodyParser

I'm trying to write a simple express server that takes incoming JSON (POST), parses the JSON and assigns to the request body. The catch is I cannot use bodyparser. Below is my server with a simple middleware function being passed to app.use
Problem: whenever I send dummy POST requests to my server with superagent (npm package that lets you send JSON via terminal) my server times out. I wrote an HTTP server in a similar fashion using req.on('data')...so I'm stumped. Any advice?
const express = require('express');
const app = express();
function jsonParser(req, res, next) {
res.writeHead(200, {'Content-Type:':'application/json'});
req.on('data', (data, err) => {
if (err) res.status(404).send({error: "invalid json"});
req.body = JSON.parse(data);
});
next();
};
app.use(jsonParser);
app.post('/', (req, res) => {
console.log('post request logging message...');
});
app.listen(3000, () => console.log('Server running on port 3000'));
I think the problem like to get rawBody in express.
Just like this:
app.use(function(req, res, next){
var data = "";
req.on('data', function(chunk){ data += chunk})
req.on('end', function(){
req.rawBody = data;
req.jsonBody = JSON.parse(data);
next();
})
})
And you need catch the error when parse the string to json and need to judge the Content-type of the Req.
Good luck.
another way that worked with me by collecting all chunks into an array and parsing the concatenated chunks.
app.use("/", (req, res, next)=>{
const body = [];
req.on("data", (chunk) => {
console.log(chunk);
body.push(chunk);
});
req.on("end", () => {
const parsedBody = Buffer.concat(body).toString();
const message = parsedBody.split('=')[1];
console.log(parsedBody);
console.log(message);
});
console.log(body);
});
To get access to req.body this worked for me:
app.use(express.json({extended: false}));
In Express v4.16.0 onwards:
app.use(express.json())