简体   繁体   English

快速路由不起作用-Node.js

[英]Express route not working - nodejs

I have a simple scenario. 我有一个简单的场景。 I am following Max tutorial. 我正在关注Max教程。

My http://localhost:3000/message always returns index page. 我的http://localhost:3000/message总是返回索引页。 That is only the first route is working. 那只是第一条路线在起作用。 The new route is not working. 新路线不起作用。 I am simply trying to put node.hbs on /message 我只是想将node.hbs放在/message

/routes/app.js /routes/app.js

var express = require('express');
var router = express.Router();

router.get('/', function (req, res, next) {
    res.render('index');
});
router.get('/messsage', function (req, res, next) {
    res.render('node', { message: 'hello' });
});
module.exports = router;

app.js app.js

var appRoutes = require('./routes/app');
app.use('/', appRoutes);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    return res.render('index');
});

Your code is working. 您的代码正在运行。 The requested URL http://localhost:3000/message is not matching any of your declared paths so it is defaulting to your custom 404 page which is the same as your index page. 请求的URL http://localhost:3000/message与您声明的任何路径都不匹配,因此它默认为与索引页面相同的自定义404页面。 Without changing your code and simply requesting http://localhost:3000/messsage will match the path of /messsage on your router. 无需更改代码,只需请求http://localhost:3000/messsage匹配路由器上/messsage的路径。 It's a typo. 这是一个错字。 😉 😉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM