繁体   English   中英

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

[英]Express route not working - nodejs

我有一个简单的场景。 我正在关注Max教程。

我的http://localhost:3000/message总是返回索引页。 那只是第一条路线在起作用。 新路线不起作用。 我只是想将node.hbs放在/message

/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

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');
});

您的代码正在运行。 请求的URL http://localhost:3000/message与您声明的任何路径都不匹配,因此它默认为与索引页面相同的自定义404页面。 无需更改代码,只需请求http://localhost:3000/messsage匹配路由器上/messsage的路径。 这是一个错字。 😉

暂无
暂无

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

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