【问题标题】:Express route not working - nodejs快速路线不起作用 - nodejs
【发布时间】:2018-05-22 15:14:30
【问题描述】:

我有一个简单的场景。我正在关注 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 包含“消息”时,您的路由路径中有错字:“消息”。 ??????

标签: node.js express


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-11-04
    • 2013-05-29
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 2019-06-08
    相关资源
    最近更新 更多