无法使用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

查看:127
本文介绍了无法使用OS X 10.11.4,Node.js获取res.render渲染。 Express3车把的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这一切都很陌生。我已经通过几个不同的教程将会得到相同的结果。我觉得有些东西与设置不兼容,或者我发胖了。以下是我相信的相关文件。我可以让res.send工作,但res.render不会渲染。我什至不能让它发出500或404错误。它似乎找到 http:// localhost:3000 ,尽管没有文字显示。我使用的是OS X 10.11.4,Node.js. Express3车把。

$ p $目录
/ Users / williamshaw / WebProjects / sicmobile williamshaw $ ls

package.json resources sicmobile.js views

bin node_modules公共路线供应商




sicmobile.js:

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

//设置句柄视图引擎
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'));
$ b $ app.get('/',function(req,res){
res.render('home');
});
app.get('/ about',function(req,res){
res.render('about');
});

// 404捕获所有处理程序(中间件)
app.use(函数(req,res,next){
res.status(404);
res.render('404');
});

// 500错误处理程序(中间件)
app.use(函数(err,req,res,next){
console.error(err.stack);
res.status(500);
res.render('500');
});
$ b app.listen(app.get('port'),function(){
console.log('Express on http:// localhost:'+
app .get('port')+';按Ctrl-C终止。');
});

main.handlebars:

<!doctype html>
< html>
< head>
< title>公司> / title>
< / head>
< body>
< div style ='position:absolute; top:0; height:40px;'>
< h3>这是头< / 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>欢迎来到Shaw Investment Company< / h1>

about.handlebars:
< h1>这是关于页面。< / h1>

400.handlebars:

< h1> 404 - 未找到< / h1>

500.handlebars:
< h1> 500 - 服务器错误< / h1>


解决方案

main.handlebars 。

 < title>公司< / title> 

如果您在开发人员控制台中检查页面元素,您会在标题标签中看到数据。 / p>

希望它有帮助。


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>

解决方案

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.

这篇关于无法使用OS X 10.11.4,Node.js获取res.render渲染。 Express3车把的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆