Can't get res.render to render using OS X 10.11.4, Node.js. Express3-handlebars

风流意气都作罢 提交于 2019-12-11 13:56:08

问题


I am new to all this. I've worked through several different tutorials will the same results. I feel like something isn't compatible with the setup or I've fat fingered something. Below are what I believe to be the relevant files. I can get res.send to work, but res.render will not render. I can't even get it to throw a 500 or 404 error. It does seem to find http://localhost:3000, though no text is displayed. I'm using OS X 10.11.4, Node.js. Express3-handlebars.

directory:
/Users/williamshaw/WebProjects/sicmobile

Williams-MacBook:sicmobile williamshaw$ ls

package.json    resources   sicmobile.js    views

bin node_modules    public  routes  vendors




sicmobile.js:

var path = require('path');
var express = require('express');

// set up handlebars view engine
var exphbs = require('express3-handlebars');
var app = express();

app.set('views', path.join(__dirname, '/views'));
app.engine('handlebars',
exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');

app.set('port', process.env.PORT || 3000);
app.use(express.static(__dirname + '/public'));

app.get('/', function(req, res) {
res.render('home');
});
app.get('/about', function(req,res){
res.render('about');
});

// 404 catch-all handler (middleware)
app.use(function(req, res, next){
res.status(404);
res.render('404');
});

// 500 error handler (middleware)
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 started on http://localhost:' +
   app.get('port') + '; press Ctrl-C to terminate.' );
});

main.handlebars:

<!doctype html>
<html>
<head>
  <title>The Company>/title>
</head>
<body>
  <div style='position: absolute; top: 0; height: 40px;'>
    <h3> This is the header</h3>
  </div>

</div style='margin-top: 60px;'>
      {{{ body }}}
  </div>
<div style='position: absolute; bottom: 0;height:40px;'>
</div>

</body>
</html>

home.handlebars:

<!doctype html>
<h1>Welcome to Shaw Investment Company</h1>

about.handlebars:
<h1>This is the  About page.</h1>

400.handlebars:

<h1>404 - Not Found</h1>

500.handlebars:
<h1>500 - Server Error</h1>

回答1:


You have typing error in main.handlebars.

<title>The Company</title>

If you check the page element in Developer Console, you will see the data in title tag itself.

Hope, it helps.



来源:https://stackoverflow.com/questions/37670488/cant-get-res-render-to-render-using-os-x-10-11-4-node-js-express3-handlebars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!