简体   繁体   中英

Node.js script is not running

Problem Statement: When I am running node.js I am getting an error

Error: Most middleware (like bodyParser) is no longer bundled with Express and must be installed separately. Please see https://github.com/

Expected Result: The page should display and the user can insert data to the DB.

Here is my Code:

filter.js

var express = require('express'),
http = require('http'),
fs = require('fs'),
bodyParser = require('body-parser'),

io = require('socket.io'),
filter = express();

mongoose = require('mongoose');

//all environments
filter.set('port', process.env.PORT || 3000);
filter.set('views', __dirname + '/views');
filter.set('view engine', 'jade');

filter.use(express.bodyParser());
filter.use(express.methodOverride());
//filter.use(filter.router);
filter.use(express.static(path.join(__dirname,'public')));

mongoose.connect('mongodb://localhost/Company');

var Schema = new mongoose.Schema({
_emailid: String,
name: String,
age: Number
});

var user = mongoose.model('emp', Schema);

filter.get('/view',  function(req, res){
user.find({}, function(err, docs){
    if(err) res.json(err);

})
});
filter.post('/new', function(res, req){
new user({
    _emailid:req.body.email,
    name:req.body.name,
    age:req.body.age,
}).save(function(err, doc){
    if(err) res.json(err);
    else res.redirect('/view');
});
});

var server = http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port' + app.get('port'));
});

Here is my HTML code:

<html>
<head>
<title>Please enter your details</title>
</head>
<body>
<h3>Please enter your details</h3>
<p>Please register below!!!</p>

<form action="/new" method="POST">

<label for="email">Email: </label>
    <input type="email" name="email" /><br>
<label for="name">Name: </label>
    <input type="text" name="name" /><br>
<label for="age">Age: </label>
    <input type="number" name="age" /><t>
    <input type="number" name="age" /><br>

<input type="submit" />
</form>
</body>

</html>

There are a number of changes with express 4.x. Like the error says, all of the middleware has been removed.

Update your package.json to include the "new" packages, a basic list can be found here and a full list, https://github.com/expressjs

Basically you need to edit

filter.use(express.bodyParser());

And replace it by,

filter.use(bodyParser());

You will have to do it for the methodOverride middleware as well.

Make sure you have all modules insatlled.

Hope this helps you,

var express = require('express'),
http = require('http'),
fs = require('fs'),
bodyParser = require('body-parser'),
methodOverride=require('method-override'),
io = require('socket.io'),
filter = express();

mongoose = require('mongoose');

//all environments
filter.set('port', process.env.PORT || 3000);
filter.set('views', __dirname + '/views');
filter.set('view engine', 'jade');

filter.use(bodyParser());
filter.use(methodOverride());
//filter.use(filter.router);
filter.use(express.static(path.join(__dirname,'public')));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM