I am writing the backend of a simple app using node and express. This is how I have structured my code:
app.js
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.json());
var router = require('./router')(app);
app.listen(80,function(){
console.log("Started on PORT 80");
})
module.exports = app;
Under subdirectory /router I have index.js
module.exports = function (app) {
app.use('/login', require('./routes/login'));
};
Under /router/routes I have login.js
var express = require('express');
var router = express.Router();
router.post('/', function(req, res){
var user = req.body.user;
console.log("Handler for /login called");
console.log("The username is: "+ user);
});
module.exports = router;
I am testing using
curl -X POST -d '{"user":"alpha", "password":"beta"}' http:my-app-ip/login --header "Content-Type:application/json"
The console prints The username is: undefined
instead of the value for key user.
What am i doing wrong?