404 Not Found when use nodejs to localhost - javascript

I made a simple login system using node and express 4.x. But I write localhost:3000 and see "Not Found 404".I think the problem is the epxress viersion is 4.x,however some code is express 3.x.
This is the error page :
Not Found
404
Error: Not Found
at app.use.res.render.message (C:\Users\Administrator\Desktop\front\node\node_web\blog\app.js:34:13)
at Layer.handle [as handle_request] (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\index.js:312:13)
at C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\index.js:271:10)
at cookieParser (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\cookie-parser\index.js:48:5)
at Layer.handle [as handle_request] (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\index.js:312:13)
at C:\Users\Administrator\Desktop\front\node\node_web\blog\node_modules\express\lib\router\index.js:280:7
app.js
var path = require('path');
var express = require('express');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var settings = require('./settings');
var users = require('./routes/users');
var session = require('express-sessi
on');
var MongoStore = require('connect-mongo')(session);
var app = express();
// view engine setup
app.set('port',process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
//app.use(routes);
// app.use(users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
app.use(session({
secret: settings.cookieSecret,
key: settings.db,//cookie name
cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days
store: new MongoStore({
db: settings.db,
host: settings.host,
port: settings.port
})
}));
app.use(express.static(path.join(__dirname,'public')));
routes(app);
module.exports = app;
index.js
module.exports = function(app) {
app.get('/', function (req, res) {
res.render('index');
});
app.get('/reg', function (req, res) {
res.render('reg', { title: '注册' });
});
app.get('/reg', function (req, res) {
});
app.post('/login', function (req, res) {
res.render('login', { title: '登录' });
});
app.post('/login', function (req, res) {
});
app.get('/post', function (req, res) {
res.render('post', { title: '发表' });
});
app.post('/post', function (req, res) {
});
app.get('/logout', function (req, res) {
});
};

In your app.js you are loading the routes but you are not telling express to use it. Put app.use('/', routes); right before the 404 catch.
PS: It's the line you've got commented.

Related

Why am i getting the 404 error in express?I have created the files correctly in views and routes folders

Note: Initially it was working for signup.js but now after i changed its file name even it is showing the same error.
Here is the app.js code:
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var expressValidator = require('express-validator');
var expressSession = require('express-session');
var usersRouter = require('./routes/users');
var signup = require('./routes/sign_up');
var user_login = require('./routes/log_in');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(bodyParser.urlencoded({extended : true}));
app.use(bodyParser.json());
app.use(expressValidator());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(expressSession({secret:'max', saveUninitialized:false, resave: false}));
console.log(signup);
app.use('/', signup);
app.use('/users', usersRouter);
app.use('/login', user_login);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
This is the log_in.js code
var express = require('express');
var router = express.Router();
const mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'abhiraj',
database : 'nodelogin'
});
connection.connect((err) => {
if (err) throw err;
console.log('Connected!');
});
/* GET login page. */
router.get('/login', function(req, res, next) {
res.render('log_in', { message: null });
req.session.errors = null;
});
router.post('/auth', function(request, response) {
var username = request.body.username;
var password = request.body.password;
connection.query('SELECT * FROM accounts WHERE username = ? AND password = ?', [username, password], function(error, results, fields) {
if (results.length > 0){
if(results[0].username===username && results[0].password===password)
request.session.loggedin = true;
request.session.username = username;
response.send('Logged in');
// response.redirect('/'+results[0].id);
response.end();
} else {
console.log('incorrect-login');
response.render('log_in', { message: "Incorrect Credentials" });
}
response.end();
});
});
module.exports = router;
And this is the error:
Not Found
404
NotFoundError: Not Found
at c:\Users\Abhiraj Kale\Documents\JS\.vscode\Node.js\mysite\app.js:34:8
at Layer.handle [as handle_request] (c:\Users\Abhiraj Kale\Documents\JS\.vscode\Node.js\mysite\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (c:\Users\Abhiraj Kale\Documents\JS\.vscode\Node.js\mysite\node_modules\express\lib\router\index.js:317:13)
at c:\Users\Abhiraj Kale\Documents\JS\.vscode\Node.js\mysite\node_modules\express\lib\router\index.js:284:7
at Function.process_params (c:\Users\Abhiraj Kale\Documents\JS\.vscode\Node.js\mysite\node_modules\express\lib\router\index.js:335:12)
at next (c:\Users\Abhiraj Kale\Documents\JS\.vscode\Node.js\mysite\node_modules\express\lib\router\index.js:275:10)
at c:\Users\Abhiraj Kale\Documents\JS\.vscode\Node.js\mysite\node_modules\express\lib\router\index.js:635:15
at next (c:\Users\Abhiraj Kale\Documents\JS\.vscode\Node.js\mysite\node_modules\express\lib\router\index.js:260:14)
at next (c:\Users\Abhiraj Kale\Documents\JS\.vscode\Node.js\mysite\node_modules\express\lib\router\route.js:127:14)
at Layer.handle [as handle_request] (c:\Users\Abhiraj Kale\Documents\JS\.vscode\Node.js\mysite\node_modules\express\lib\router\layer.js:91:12)
This is my file structure:
router.get('/login', function(req, res, next) {
res.render('log_in', { message: null });
req.session.errors = null;
});
As I can see from app.js your express middleware already has /login.
So you need to remove /login from express router;
So your new code should be
router.get('/', function(req, res, next) {
res.render('log_in', { message: null });
req.session.errors = null;
});

Can't get a proper error from passport-linkedin strategy

I can't seem to get some good feedback to properly diagnose the problem.
Right now I am getting a generic error I set up in the app.js file
Here is the error:
Not Found
404
Error: Not Found
at /Users/avonproductsinc/Dropbox/developer_folder/oauth-passport-securing-application/securing-app-OAuth-passport/app.js:48:13
at Layer.handle [as handle_request] (/Users/avonproductsinc/Dropbox/developer_folder/oauth-passport-securing-application/securing-app-OAuth-passport/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/avonproductsinc/Dropbox/developer_folder/oauth-passport-securing-application/securing-app-OAuth-passport/node_modules/express/lib/router/index.js:312:13)
at /Users/avonproductsinc/Dropbox/developer_folder/oauth-passport-securing-application/securing-app-OAuth-passport/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/avonproductsinc/Dropbox/developer_folder/oauth-passport-securing-application/securing-app-OAuth-passport/node_modules/express/lib/router/index.js:330:12)
at next (/Users/avonproductsinc/Dropbox/developer_folder/oauth-passport-securing-application/securing-app-OAuth-passport/node_modules/express/lib/router/index.js:271:10)
at /Users/avonproductsinc/Dropbox/developer_folder/oauth-passport-securing-application/securing-app-OAuth-passport/node_modules/express/lib/router/index.js:618:15
at next (/Users/avonproductsinc/Dropbox/developer_folder/oauth-passport-securing-application/securing-app-OAuth-passport/node_modules/express/lib/router/index.js:256:14)
at Function.handle (/Users/avonproductsinc/Dropbox/developer_folder/oauth-passport-securing-application/securing-app-OAuth-passport/node_modules/express/lib/router/index.js:176:3)
at router (/Users/avonproductsinc/Dropbox/developer_folder/oauth-passport-securing-application/securing-app-OAuth-passport/node_modules/express/lib/router/index.js:46:12)
This is my code in my linkedin strategy:
var passport = require('passport');
var LinkedInStrategy = require('passport-linkedin').Strategy;
module.exports = function() {
passport.use(new LinkedInStrategy({
consumerKey: '77z7e2bq34ad0w',
consumerSecret: 'ajTJfuNTxLnDGlCK',
callbackURL: "http://127.0.0.1:3000/auth/linkedin/callback"
},
function(token, tokenSecret, profile, done) {
var user = {};
// user.email = profile.emails[0].value;
// user.image = profile._json.image.url;
user.displayName = profile.displayName;
user.linkedin = {};
user.linkedin.id = profile.id;
user.linkedin.token = tokenSecret;
done(null, user);
}));
};
And this is in my auth.js file
var express = require('express');
var passport = require('passport');
var router = express.Router();
router.route('/linkedin')
.get(passport.authenticate('linkedin'));
router.route('/linkedin/callback')
.get(passport.authenticate('linkedin', {
successRedirect: '/users',
failureRedirect: '/error'
}));
Any help is appreciated, naturally!
Update Jan 15 2017
app.js code:
var express = require('express'),
path = require('path'),
favicon = require('serve-favicon'),
logger = require('morgan'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
passport = require('passport'),
session = require('express-session'),
mongoose = require('mongoose'),
routes = require('./routes/index'),
users = require('./routes/users'),
auth = require('./routes/auth'),
app = express(),
db = mongoose.connect('mongodb://localhost/socialAgg');
app.use(require('node-sass-middleware')({
src: path.join(__dirname, 'public'),
dest: path.join(__dirname, 'public'),
indentedSyntax: true,
sourceMap: true
}));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// setting up passport middlware
app.use(session({secret: 'anything'}));
require('./config/passport')(app);
app.use('/', routes);
app.use('/users', users);
app.use('/auth', auth);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;

How do I correctly call passport.js functions with express router?

I'm trying to call the passport.authenticate function as response to post request for my registration view. I've been following this tutorial here to get my head around passport.js and express but having completed the first page of instructions and making the necessary changes for express.js 4.0 every page I try to access returns error 500 and displays the following stacktrace:
TypeError: Object #<serverResponse> has no method 'authenticate'
at module.exports (/home/myUserName/Desktop/manufacturing/routes/index.js:66:37)
at Layer.handle [as handle_request] (/home/myUserName/Desktop/manufacturing/node_modules/express/lib/router/layer.js:82:5)
at trim_prefix (/home/myUserName/Desktop/manufacturing/node_modules/express/lib/router/index.js:302:13)
at /home/myUserName/Desktop/manufacturing/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/home/myUserName/Desktop/manufacturing/node_modules/express/lib/router/index.js:321:12)
at next (/home/myUserName/Desktop/manufacturing/node_modules/express/lib/router/index.js:261:10)
at /home/myUserName/Desktop/manufacturing/node_modules/connect-flash/lib/flash.js:21:5
at Layer.handle [as handle_request] (/home/myUserName/Desktop/manufacturing/node_modules/express/lib/router/layer.js:82:5)
at trim_prefix (/home/myUserName/Desktop/manufacturing/node_modules/express/lib/router/index.js:302:13)
at /home/myUserName/Desktop/manufacturing/node_modules/express/lib/router/index.js:270:7
Here is the code for /routes/index.js:
var express = require('express');
var bodyParser = require('body-parser');
var router = express.Router();
module.exports = function(app, passport) {
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', {
title: 'Home Page'
});
});
/* GET about page */
router.get('/about', function(req, res, next) {
res.render('about', {
title: 'About Us'
});
});
/* GET contact page */
router.get('/contact', function(req, res, next) {
res.render('contact', {
title: 'Contact Us'
});
});
/* GET login page */
router.get('/login', function(req, res, next) {
res.render('login', {
title: 'User Login',
message: req.flash('loginMessage')
});
});
/* GET register page */
router.get('/register', function(req, res, next) {
res.render('register', {
title: 'User Registration',
message: req.flash('signupMessage')
});
});
/* POST register page */
router.post('/register', passport.authenticate('local', {
successRedirect : '/',
failureRedirect : '/register',
failureFlash : true
}));
/* Log Out */
router.get('/logout', function(req, res, next) {
req.logout();
res.redirect('/');
});
/* 404 - page not found */
router.get('*', function(req, res){
res.render('404', {
title: '404 - Not Found'
});
});
// Route middleware to make sure a user is logged in
function isLoggedIn(req, res, next) {
if (req.isAuthenticated())
return next();
res.redirect('/');
};
};
And here is my server.js:
var express = require('express');
var path = require('path');
var logger = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var passport = require('passport');
var flash = require('connect-flash');
// Database
var configDB = require('./config/database.js');
mongoose.connect(configDB.url);
require('./config/passport')(passport);
// Routes
var routes = require('./routes/index');
var port = process.env.PORT || 3000;
var app = express();
// Express configuration
app.use(logger('dev')); // log every request to console
app.use(bodyParser.json()); // get information from html forms
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser()); // read cookies (needed for auth)
app.use(express.static(path.join(__dirname, 'public')));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
if (app.get('env') === 'development') {
app.locals.pretty = true;
}
// Passport configuration
app.use(session({ secret: 'MySecretHere',
saveUninitialized: true,
resave: true})); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
app.use('/', routes);
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
app.listen(port);
console.log('Server running on ' + port);
The reason your are getting the error is because your exporting a function that takes app and passport as a parameters for your http handlers, but when your require it in your server.js your are not passing passport or app.
You need to somehow pass passport into the require.
You could change your routes files to
If your are using the express.Router
var router = express.Router();
module.exports = function(passport) {
// init your routes with passport and router
router.post('/' function(req, res) {
//...
});
// make sure to return router in the function though
return router;
}
If not using express.Router you could pass both app and passport and simply attach handlers to app
module.exports = function (app, passport) {
app.post('/', function(req, res) {
//...
});
// no need to return app
}
then in server.js
// if your using `express.Router()` in routes file
app.use('/', require('./routes')(passport));
// If your registering handler on app itself just bootstrap it
require('./routes')(app, passport);
You need to pass passport to your route. You can do it changing your code this way:
/routes/index.js
from module.exports = function(app, passport) {...}
to
module.exports = function(passport){ return function(app) {...}}
/server.js
from app.use('/', routes);
to app.use('/', routes(passport));

express router not found

I created an express project using the express generator like following
express project_name
and
npm install
This is my app.js
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
These are my routes
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'iDanG Management' });
});
module.exports = router;
I have following index.jade
extends layout
block content
div.container
div.row
form(id='myform' action='' method='post')
p Name:
input(type='text' name='name')
p E-Mail:
input(type='text' name='email')
input(type='submit' value='add')
table(class='table table-hover' id='user_table')
thead
tr
th Name
th Email
tbody
script.
$(document).ready(
function() {
$('#user_table').dataTable({
"pagingType": "simple_numbers",
"ordering": false
});
$('#myform').ajaxForm({ beforeSubmit:
function (formData, jqForm, options) {
$('#table').DataTable().row.add([formData[0].value, formData[1].value]).draw();
return false;
}
});
});
And following layout.jade
doctype html
html
head
title= title
script(src="/javascripts/jquery-1.11.2.min.js")
script(src="/javascripts/jquery.form.js")
script(src="/javascripts/jquery.dataTables.min.js")
link(rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css")
link(rel="stylesheet" href="/javascripts/jquery.dataTables.min.css")
body
block content
when I try to submit that form I receive following error:
Error: Not Found
at app.use.res.render.message (/home/ubuntu/iDanG_Management/app.js:30:13)
at Layer.handle [as handle_request] (/home/ubuntu/iDanG_Management/node_modules/express/lib/router/layer.js:82:5)
at trim_prefix (/home/ubuntu/iDanG_Management/node_modules/express/lib/router/index.js:302:13)
at /home/ubuntu/iDanG_Management/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/home/ubuntu/iDanG_Management/node_modules/express/lib/router/index.js:321:12)
at next (/home/ubuntu/iDanG_Management/node_modules/express/lib/router/index.js:261:10)
at /home/ubuntu/iDanG_Management/node_modules/express/lib/router/index.js:603:15
at next (/home/ubuntu/iDanG_Management/node_modules/express/lib/router/index.js:246:14)
at Function.proto.handle (/home/ubuntu/iDanG_Management/node_modules/express/lib/router/index.js:166:3)
at router (/home/ubuntu/iDanG_Management/node_modules/express/lib/router/index.js:35:12)
For the forms I used this
Do you have any suggestions how I can solve this problem? Help is highly appreciated!
It seems like you are doing a post request and your routes does not have post handler.
May be add route for post:
router.post('/', function(req, res, next) {
//do something and return data here?
});

Node.js & MongoDB - error 404 -not found

I'm trying to run an node.js and mongodb application...
see: http:// 93.188.162.100:3000/MelhoraCidade/ws/mcapp
but i'm getting the following error
Not Found
404
Error: Not Found
at app.use.res.render.message (/home/ladessa/files/MelhoraCidade/ws/mcapp/app.js:30:15)
at Layer.handle [as handle_request] (/home/ladessa/files/MelhoraCidade/ws/mcapp/node_modules/express/lib/router/layer.js:82:5)
at trim_prefix (/home/ladessa/files/MelhoraCidade/ws/mcapp/node_modules/express/lib/router/index.js:271:13)
at /home/ladessa/files/MelhoraCidade/ws/mcapp/node_modules/express/lib/router/index.js:238:9
at Function.proto.process_params (/home/ladessa/files/MelhoraCidade/ws/mcapp/node_modules/express/lib/router/index.js:313:12)
at /home/ladessa/files/MelhoraCidade/ws/mcapp/node_modules/express/lib/router/index.js:229:12
at Function.match_layer (/home/ladessa/files/MelhoraCidade/ws/mcapp/node_modules/express/lib/router/index.js:296:3)
at next (/home/ladessa/files/MelhoraCidade/ws/mcapp/node_modules/express/lib/router/index.js:190:10)
at /home/ladessa/files/MelhoraCidade/ws/mcapp/node_modules/express/lib/router/index.js:192:16
at Function.match_layer (/home/ladessa/files/MelhoraCidade/ws/mcapp/node_modules/express/lib/router/index.js:296:3)
I used "express mcapp" to create the app....
EDIT app.js code:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;

Categories