Пример из книги:
var express = require('express');
var app = express();
var handlebars = require('express-handlebars').create({ defaultLayout: 'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', process.env.PORT || 3000);
app.get('/', function(req, res) {
res.render('home');
});
app.get('/about', function(req, res) {
res.render('about');
});
// user page 404
app.use(function(req, res, next) {
res.status(404);
res.render('404');
});
// user page 500
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500);
res.render('500');
});
app.listen(app.get('port'), function() {
console.log('Express start on http://localhost:' +
app.get('port') + '; push Ctrl+C for stop.');
});
Запускается, но при обращении к браузеру вываливается:
Error: Failed to lookup view "home" in views directory "/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/views"
at Function.render (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/application.js:579:17)
at ServerResponse.render (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/response.js:960:7)
at /home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/meadowlark.js:12:7
at Layer.handle [as handle_request] (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/layer.js:95:5)
at next (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/layer.js:95:5)
at /home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/index.js:330:12)
at next (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/index.js:271:10)
Error: Failed to lookup view "500" in views directory "/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/views"
at Function.render (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/application.js:579:17)
at ServerResponse.render (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/response.js:960:7)
at /home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/meadowlark.js:29:7
at Layer.handle_error (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/index.js:310:13)
at /home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/index.js:330:12)
at next (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/index.js:271:10)
at Layer.handle_error (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/layer.js:67:12)
at trim_prefix (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/index.js:310:13)
Сообщение в браузере:
Error: Failed to lookup view "500" in views directory "/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/views"
at Function.render (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/application.js:579:17)
at ServerResponse.render (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/response.js:960:7)
at /home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/meadowlark.js:29:7
at Layer.handle_error (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/layer.js:71:5)
at trim_prefix (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/index.js:310:13)
at /home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/index.js:330:12)
at next (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/index.js:271:10)
at Layer.handle_error (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/layer.js:67:12)
at trim_prefix (/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/node_modules/express/lib/router/index.js:310:13)