我正在使用安装了express和express-handlebars包的 Node 创建一个页面。我正在尝试在所有页面上创建一个菜单,通过该菜单我可以通过单击按钮跳转到页面。我的前 2 个超链接工作正常,然后当我决定添加 aboutMe 页面和测试页面时,它们都重定向到 404。 文件结构
静态.js
|
Views
---home.handlebars
---404.handlebars
---other-page.handlebars
---aboutMe.handlebars
---testing.handelbars
Layouts
|
main.handlebars
我的 static.js 上的代码:
var express = require('express');
var app = express();
var handlebars =require('express- handlebars').create({defaultLayout:'main'});
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', 3000);
app.get('/', function(req,res){
res.render('home');
}
app.get('/other-page', function(req,res){
res.render('other-page');
});
app.get('/aboutMe', function(req,res){
res.render('aboutMe');
});
app.get('/testing', function(req,res){
res.render('testing');
});
app.use(function(req,res){
res.status(404);
res.render('404');
});
app.use(function(err,req,res,next){
console.error(err.stack);
res.type('plain/text');
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.');
});
来 self 的 main.handlebars 的片段,其中我链接: 家 其他页面 关于 测试
最佳答案
我们会的,经过几个小时的努力,我已经解决了我的问题。我意识到我的 Node 永远在 static.js 上运行,我对文件进行了更改,直到我停止并重新启动 static.js 上的 Node 时,这些更改才发生。 :)
关于javascript - Express Handlebars-页面正在路由至 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37357687/