无法使用 OS X 10.11.4,Node.js 使 res.render 呈现。 Express3-车把
Can't get res.render to render using OS X 10.11.4, Node.js. Express3-handlebars
我对这一切都是陌生的。我已经完成了几个不同的教程,结果相同。我觉得有些东西与设置不兼容,或者我的手指太粗了。以下是我认为是相关文件的内容。我可以让 res.send 工作,但 res.render 不会呈现。我什至无法让它抛出 500 或 404 错误。它似乎确实找到了 http://localhost:3000,但没有显示任何文本。我正在使用 OS X 10.11.4,Node.js。 Express3 车把。
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>
您在 main.handlebars
中输入错误。
<title>The Company</title>
如果您在 Developer Console 中查看页面元素,您会在标题标签中看到数据。
希望对你有帮助。
我对这一切都是陌生的。我已经完成了几个不同的教程,结果相同。我觉得有些东西与设置不兼容,或者我的手指太粗了。以下是我认为是相关文件的内容。我可以让 res.send 工作,但 res.render 不会呈现。我什至无法让它抛出 500 或 404 错误。它似乎确实找到了 http://localhost:3000,但没有显示任何文本。我正在使用 OS X 10.11.4,Node.js。 Express3 车把。
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>
您在 main.handlebars
中输入错误。
<title>The Company</title>
如果您在 Developer Console 中查看页面元素,您会在标题标签中看到数据。
希望对你有帮助。