A
A
Andrey Titov2017-03-01 19:51:00
JavaScript
Andrey Titov, 2017-03-01 19:51:00

What's wrong with the Express code?

Example from the book:

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.');
});

It starts, but when accessing the browser, it falls out:
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)

Browser message:
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)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
shushus, 2018-06-07
@titov_andrei

If the extension is hbs, then you need to write
in the handlebars configuration when creating specify
extname: '.hbs' of type
var handlebars = require('express-handlebars').create({extname: '.hbs'});
and in the template engine assignment:
app.engine('hbs', handlebars.engine);
app.set('view engine', 'hbs');

C
CheNet, 2017-03-06
@CheNet

I think the application was generated using express and the views folder is auto-detected. The error says that the home file cannot be found in the views folder. Based on this, questions arise, no matter how trite they sound:
1) were the home and about template files created?
2) are they in "/home/andrew/CODE/JS/DEV/node-express-tutorial-book/meadowlark/views" folder?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question