GET
// grab the packages we need var express = require('express'); var app = express(); var port = process.env.PORT || 8080; // routes will go here // start the server app.listen(port); console.log('Server started! At http://localhost:' + port);
http://example.com/api/users?id=4&token=sdfa3&geo=us
// routes will go here app.get('/api/users', function(req, res) { var user_id = req.param('id'); var token = req.param('token'); var geo = req.param('geo'); res.send(user_id + ' ' + token + ' ' + geo); });
POST
npm install body-parser --save
var bodyParser = require('body-parser'); app.use(bodyParser.json()); // support json encoded bodies app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies // POST http://localhost:8080/api/users // parameters sent with app.post('/api/users', function(req, res) { var user_id = req.body.id; var token = req.body.token; var geo = req.body.geo; res.send(user_id + ' ' + token + ' ' + geo); });
References
https://scotch.io/tutorials/use-expressjs-to-get-url-and-post-parameters
http://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm
https://www.npmjs.com/package/body-parser-json