【问题标题】:node.js app.get not being callednode.js app.get 没有被调用
【发布时间】:2020-10-07 14:37:37
【问题描述】:

total node.js noobie,开始玩各种教程和网站的演示代码,我注意到一些我不明白的东西......

也就是说,如果我的 /public 文件夹中有 index.html,那么

app.get("/", function (req, res) {
    console.log("get /");
    res.redirect("/test.html");
});

根本就不会被调用。一旦我将 index.html 重命名为 index2.html,就会调用该方法并将我重定向到 /public/test.html

这就是我所拥有的:

var io = require('socket.io'),
    express = require('express'),
    MemoryStore = express.session.MemoryStore,
    app = express.createServer(),
    sessionStore = new MemoryStore();

app.configure(function () {
    app.use(express.bodyParser());
    app.use(express.cookieParser());
    app.use(express.session({
        store: sessionStore,
        secret: 'secret',
        key: 'express.sid'
    }));
    app.use(express.static(__dirname + '/public'));
});

其余的内容大部分来自本教程:http://www.danielbaulig.de/socket-ioexpress/

任何其他文件都会出现同样的问题。如果我有 /public/test.html,那么当我打电话时

http://localhost:8201/test.html

此 app.get 未被调用:

app.get("/test.html", app.authenticateUser, function (req, res) {
    console.log("get /test.html");
    res.redirect("/test2.html");
});

当我删除 test.html 时,我会被转发到 test2.html...

我尝试重定向的原因是,如果用户未登录,我不希望他打开 index.html,而是希望将他转发到 login.html,如果 index.html 存在,这是不可能的。唯一的“解决方案”是做它糟糕的客户端,我不希望 index.html 在客户端浏览器中加载只是为了将他转发到 login.html,在我看来,服务器应该处理它。

【问题讨论】:

  • 你为什么使用静态index.html而不是视图?在我看来,你只是让自己变得更难。

标签: node.js express


【解决方案1】:

问题是您的静态文件中间件app.use(express.static(__dirname + '/public')) 在您的路由器“前面”。当你写

app.configure(function () {
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({
    store: sessionStore,
    secret: 'secret',
    key: 'express.sid'
}));
app.use(express.static(__dirname + '/public'));
});

这相当于

app.configure(function () {
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({
    store: sessionStore,
    secret: 'secret',
    key: 'express.sid'
}));
app.use(express.static(__dirname + '/public'));
app.use(app.router); //Express implicitly appends router middleware!
});

因为如果你没有明确地在某个地方添加它,Express 会在堆栈末尾隐式附加路由器中间件。现在您清楚地看到静态文件中间件在路由器前面。如果找到静态文件,则由app.use(express.static(__dirname + '/public')) 提供服务,并且永远不会调用路由器app.use(app.router)。如果您希望您的请求始终通过路由器,则应将其放在前面,例如

app.configure(function () {
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({
    store: sessionStore,
    secret: 'secret',
    key: 'express.sid'
}));
app.use(app.router); //Now router comes first and will be executed before static middleware
app.use(express.static(__dirname + '/public'));
});

【讨论】:

  • 感谢详细解答,不幸的是,当我添加 app.use(app.router);...
  • 反应很好。
  • 感谢您的解释。
  • 如果您使用的是 express 4,则不应使用 app.router。我收到此错误:“throw new Error('\'app.router\' is deprecated!\n请参阅 3.x to 4.x 迁移指南,详细了解如何更新您的应用。');"
【解决方案2】:

这是因为 express 在请求到达您的代码之前对其进行过滤。它找到文件并将其返回给浏览器。

解决方案是通过 socket.io 发送一个事件,告诉用户浏览器中的代码重定向或将文件移动到私有空间(公共目录之外)并按照 CydGy 的建议通过“fs”提供它。

【讨论】:

    【解决方案3】:

    这个非常好的教程 (http://www.danielbaulig.de/socket-ioexpress/) 处理套接字。 而且我认为对这种情况没有用。

    所以,看这个:

    app.get("/test.html", app.authenticateUser, function (req, res) {
    

    但是app.authenticateUser 在哪里? 肯定是他挡住了

    因此,将其替换为:

    app.get("/test.html", function (req, res) {
    

    或修改您的app.authenticateUser

    (并使用模块fs读取您的文件,然后,您可以res.send(file);

    不要忘记在你的url中写上.html,否则你必须用"/test"替换"/test.html"

    希望对你有帮助。

    【讨论】:

    【解决方案4】:

    这是因为下面的代码从来没有被调用过!

        app.get("/", function (req, res) {
        console.log("get /");
        res.redirect("/test.html");
    }); 
    

    作为 app.use(app.router);已折旧。您所要做的就是在此行上方编写代码

    app.use(express.static(__dirname + '/public'));
    

    应该是这样的

        app.get("/", function (req, res) {
        console.log("get /");
        res.redirect("/test.html");
        app.use(express.static(__dirname + '/public'));
    

    【讨论】:

      【解决方案5】:

      app.get("/", function (req, res)...

      试试这个

      app.get('/', 函数(req, res)...

      Uri 路径必须是 ' 单引号。

      【讨论】:

      • 这是不正确的,单引号和双引号都是有效的Javascript
      猜你喜欢
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 2016-08-19
      • 2014-10-23
      相关资源
      最近更新 更多