samedi 7 mars 2015

Nodejs doesn't recognize body-parser

Im trying to develop a nodejs app, however when I try to use the body parser the following error shows up:


"Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see http://ift.tt/1iB6hIw."


Here's my code:



var http = require('http');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');


app.use(express.static(__dirname +'/views'));
app.use(express.bodyParser());

//Start server
var server = http.createServer(app).listen(8000, function(request, response){
app.get('/', function(req, res){
res.sendfile('views/htmls/main.html');
})
app.get('/nuevo', function(req, res){
res.sendfile('views/htmls/nuevo.html');
})
app.post('/', function(req,res){
res.send('Name"' + req.body.title + '".');
})
});
console.log('Listening on http://127.0.0.1:8000');


The error shows up when I add the line: app.use(express.bodyParser());


If I comment it, I get no error.


I already tried: npm install body-parser and npm install -g body-parser


and still no fix.


How can I fix this?


Aucun commentaire:

Enregistrer un commentaire