Error while running app.js
mehul400081 opened this issue · comments
I am new to nodejs+express stack. Trying to learn it using your project as a templated. I see following error despite installting connect package seperately. Can you please guide where is the issue.
C:\Users\m\Desktop\node\angular-express-seed-master>node app.js
body-parser deprecated bodyParser: use individual json/urlencoded middlewares ap
p.js:28:9
body-parser deprecated urlencoded: explicitly specify "extended: true" for exten
ded parsing node_modules\body-parser\index.js:74:29
Error: Most middleware (like errorHandler) is no longer bundled with Express and
must be installed separately. Please see https://github.com/senchalabs/connect#
middleware.
at Function.Object.defineProperty.get (C:\Users\m\Desktop\node\angular-
express-seed-master\node_modules\express\lib\express.js:89:13)
at Object. (C:\Users\m\Desktop\node\angular-express-seed-mas
ter\app.js:36:19)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
In your app.js file on line 36, there should be a reference to body-parser, change that to body-parser.json. If that doesn't work, please paste the code in app.js here. Good luck
There is just 1 reference to body parser with require() function. The change did not work. Here is the app.js pulled from Git.
/*
- Module dependencies
*/
var express = require('express'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
errorHandler = require('error-handler'),
morgan = require('morgan'),
routes = require('./routes'),
api = require('./routes/api'),
http = require('http'),
path = require('path');
var app = module.exports = express();
/**
- Configuration
*/
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(morgan('dev'));
app.use(bodyParser());
app.use(methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
var env = process.env.NODE_ENV || 'development';
// development only
if (env === 'development') {
app.use(express.errorHandler());
}
// production only
if (env === 'production') {
// TODO
}
/**
- Routes
*/
// serve index and view partials
app.get('/', routes.index);
app.get('/partials/:name', routes.partials);
// JSON API
app.get('/api/name', api.name);
// redirect all others to the index (HTML5 history)
app.get('*', routes.index);
/**
- Start Server
*/
http.createServer(app).listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});
Read the first line of your error: line 28.
Change it to:
app.use(bodyParser.json());
Note - it still doesnt work after this change but now its on to the next bug with errorHandler...
Ok, I think I figured out the remaining issues...
run: npm install express-error-handler
change line 9 to: errorHandler = require('express-error-handler'),
change line 36 to: app.use(errorHandler());
The app should run now.
Thanks its working now.
Super helpful thanks all!
Thanks @justinireland!!! It is really helpful!!!
excelent @justinireland thanx
Thank you justinireland ...you just saved my day 👍
Wtf does this actually fix? Wtf even is express-error-handler? Express/BodyParser is BROKEN atm..