Skip to content

Instantly share code, notes, and snippets.

@TinyExplosions
Created January 15, 2018 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TinyExplosions/969d22fc6977ad72b46918c4b6364305 to your computer and use it in GitHub Desktop.
Save TinyExplosions/969d22fc6977ad72b46918c4b6364305 to your computer and use it in GitHub Desktop.
var express = require('express');
var bodyParser = require('body-parser');
var cors = require('cors');
function helloRoute() {
var hello = new express.Router();
hello.use(cors());
hello.use(bodyParser.urlencoded({
extended: true
}));
hello.use(bodyParser.json());
// GET REST endpoint - query params may or may not be populated
hello.get('/', function(req, res) {
console.log(new Date(), 'In hello route GET / req.query=', req.query);
var world = req.query && req.query.hello ? req.query.hello : 'World';
// see http://expressjs.com/4x/api.html#res.json
res.json({ msg: 'Hello ' + world });
});
// POST REST endpoint - note we use 'body-parser' middleware above to parse the request body in this route.
// This can also be added in application.js
// See: https://github.com/senchalabs/connect#middleware for a list of Express 4 middleware
hello.post('/', function(req, res) {
console.log(new Date(), 'In hello route POST / req.body=', req.body);
var world = req.body && req.body.hello ? req.body.hello : 'World';
// see http://expressjs.com/4x/api.html#res.json
res.json({ msg: 'Hello ' + world });
});
return hello;
}
module.exports = helloRoute;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment