Martin K Martin K - 5 months ago 41
Node.js Question

Why does the call to node.js body-parser fail despite the fact that I have installed it?

I'm beginning to learn node.js and trying to work out how to get the contents of a POST request. I am trying to follow the instructions in this post.
So far I have successfully installed node.js (on Windows 7) and express, and been able to get my very first script to work. However my problem comes when I try to use body-parser. I have installed it and it appears to be there (here is a screenshot)

Here is the code of the node.js script

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(express.json()); // to support JSON-encoded bodies

app.get('/', function(req, res) {
res.setHeader('Content-Type', 'text/plain');
res.end('Vous êtes à l\'accueil');
});

app.get('/user/:usernum', function(req, res) {
res.setHeader('Content-Type', 'text/plain');
res.end('You are on page USER with n° : ' + req.params.usernum);
});

// http://stackoverflow.com/questions/5710358/how-to-get-post-a-query-in-express-js-node-js
app.post('/adonis', function(req, res) {
res.setHeader('Content-Type', 'text/plain');
console.log(req.body.title);
// res.write(JSON.stringify(req));
res.end('Hopefully I stringified a POST');
});

// ... Tout le code de gestion des routes (app.get) se trouve au-dessus

app.use(function(req, res, next){
res.setHeader('Content-Type', 'text/plain');
res.status(404).send('Page introuvable !');
});

app.listen(8091);


Yet when I run it, node.js throws an error saying "cannot find module body-parser". What have I done wrong?

As per @Kale's and others' suggestions I tried installing body-parser locally, but this does not seem to help since now my script gives the following message:

Error: Most middleware (like json) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
at Function.Object.defineProperty.get (d:\smartguide\nodejs\node_modules\express\lib\express.js:99:13)
at Object.<anonymous> (d:\smartguide\nodejs\oc1.js:5:16)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3`


I tried installing "json" locally and globally - the install seems to work but it makes no difference to the file error.

Answer

I think I was doing something fundamentally wrong - I went back to basics and started all over again, this time making sure I had a package.json file. Now it works.

Here is the code:

var express = require('express');
var session = require('cookie-session');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var jsonParser = bodyParser.json();

var app = express();


// JSON testing
app.post('/json-test', jsonParser, function(req, res) {
    if (!req.body) return res.sendStatus(400);
    console.log(JSON.stringify(req.body));
    console.log(req.body.title);
    res.status(200).send(req.body.title);
    })

// Can't get anything else
.use(function(req, res, next){
    res.setHeader('Content-Type', 'text/plain');
    res.status(404).send('Page introuvable !');
    })


.listen(8090);

And here is the package.json

{
"name": "todo1",
"version": "0.1.0",
"dependencies": {
    "express": "~4.11.0",
    "ejs": "~2.1.4",
    "cookie-session": "~1.1.0",
    "body-parser": "~1.10.1"
},
"author": "Martin",
"description": "Un gestionnaire de todolist ultra basique"
}