Node.js 快速路由器-为什么带有参数的路由不能按预期工作?

Node.js 快速路由器-为什么带有参数的路由不能按预期工作?,node.js,express,Node.js,Express,我对此感到困惑。为什么我可以在url上看到这一点,但不能在url上看到?它在发短信,对吗?res.send(向页面发送“hello”)。但是adminRouter.get肯定应该拉第二个url(路径中有“用户”一词)?它基本上做了与我期望的相反的事情 下面是代码片段 // route with parameters (http://localhost:1337/admin/users/:name) adminRouter.get('/users/:name', function(req,

我对此感到困惑。为什么我可以在url上看到这一点,但不能在url上看到?它在发短信,对吗?res.send(向页面发送“hello”)。但是adminRouter.get肯定应该拉第二个url(路径中有“用户”一词)?它基本上做了与我期望的相反的事情

下面是代码片段

// route with parameters (http://localhost:1337/admin/users/:name)
    adminRouter.get('/users/:name', function(req, res) {
        res.send('hello ' + req.params.name + '!');
    });
**编辑:以下是其他路线的全部代码:

// load the express package and create our app
var express = require('express');
var app     = express();

// send our index.html file to the user for the home page
app.get('/', function(req, res) {
    res.sendFile(__dirname + '/index.html');
});

    // get an instance of the router
    var adminRouter = express.Router();

   // route middleware that will happen on every request
    adminRouter.use(function(req, res, next) {

        // log each request to the console
        console.log(req.method, req.url);

        // continue doing what we were doing and go to the route
        next(); 
    });
// route middleware to validate :name
    adminRouter.param('name', function(req, res, next, name) {
        // do validation on name here
        // blah blah validation
        // log something so we know its working
        console.log('doing name validations on ' + name);
        // once validation is done save the new item in the req
        req.name = name;
        // go to the next thing
        next();
    });
// route with parameters (http://localhost:1337/admin/users/:name)
    adminRouter.get('/users/:name', function(req, res) {
        res.send('hello ' + req.params.name + '!');
    });

    // create routes for the admin section
    // admin main page. the dashboard
    adminRouter.get('/', function(req, res) {
        res.send('I am the dashboard!');
    });

    // users page
    adminRouter.get('/users', function(req, res) {
        res.send('I show all the users!');
    });

    // posts page
    adminRouter.get('/posts', function(req, res) {
        res.send('I show all the posts!');
    });

    // apply the routes to our application
    app.use('/admin', adminRouter);

// start the server
app.listen(1337);
console.log('1337 is the magic port!');

所以这个问题的答案就是手动重启我的服务器,然后问题就自行解决了


为什么它自己陷入了困境,并且做了与它应该做的相反的事情谁知道呢,但是在重新启动服务器时,它工作正常。

您还有其他途径吗?它们是什么?在这条路线之前可能有另一条路线匹配。是的,啊,很有趣,请参阅我上面的编辑(添加了代码)。很抱歉,我无法理解您的问题。你是说,你不能打开吗?还有,哪个版本的Express?我已经一字不差地复制了您的代码,并使用Express4.12.4运行了它,它似乎可以工作。不起作用,而且确实起作用(打印“hello holly!”),这与您描述的相反。Express 4.12.4。这太奇怪了,我停止并重新启动了服务器,现在它正按照我认为应该的方式工作。现在,如果我在url中使用“hello”一词,我会得到错误
无法获取/admin/hello/holly
,如果我在url中使用“users”,它会打印“hello holly!”转到页面。所以,它现在正在按预期工作。为什么在我重新启动之前它会做相反的事情?很奇怪。终端还说每次我保存文件时服务器都会重新启动,所以,我很惊讶手动停止并重新启动修复了它。不,比丹,我的意思正好相反,但它现在起作用了。非常感谢。