快速静态目录如何与 404 路由配合使用?

发布于 2024-12-11 06:33:50 字数 619 浏览 0 评论 0原文

我有一些代码如下所示:

app.configure(function() {
   app.set("views", __dirname + "/views");
   app.set("view engine", "ejs");
   app.use(express.bodyParser());
   app.use(express.methodOverride());
   app.use(express.logger()); 
   app.use(app.router);
   app.use(express.static(__dirname + "/public"));
});

//Routes
app.get("/", function(req, res) {
    res.render("index.ejs", {locals: {
      title: "Welcome"
    }});
});

//Handle 404
app.get("/*", function(req, res, next) {
    next("Could not find page");
});

我遇到的问题是我无法访问 /public 静态目录中的任何内容:所有内容都被 404 路由捕获。我是否遗漏了一些关于它应该如何工作的信息?

I have some code that looks like the following:

app.configure(function() {
   app.set("views", __dirname + "/views");
   app.set("view engine", "ejs");
   app.use(express.bodyParser());
   app.use(express.methodOverride());
   app.use(express.logger()); 
   app.use(app.router);
   app.use(express.static(__dirname + "/public"));
});

//Routes
app.get("/", function(req, res) {
    res.render("index.ejs", {locals: {
      title: "Welcome"
    }});
});

//Handle 404
app.get("/*", function(req, res, next) {
    next("Could not find page");
});

The problem I have is that I can't access anything in the /public static directory: everything gets caught by the 404 route. Am I missing something about how this is supposed to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

離人涙 2024-12-18 06:33:50

你正在做的

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

事情你想要做的是,

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

因为你在 app.router 中有一个捕获所有路由,所以它必须比其他任何东西都低。否则,catch all 路由确实会捕获所有内容,而中间件的其余部分将被忽略。

顺便说一句,所有这样的路线都很糟糕。

You're doing

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

What you want to do is

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

Since you have a catch all route in app.router it must be lower then anything else. otherwise the catch all route will indeed catch everything and the rest of the middleware is ignored.

As an aside catch all routes like that are bad.

稚气少女 2024-12-18 06:33:50

更好的解决方案是将以下代码放在对 app.use 的所有调用之后:

app.use(function(req, res) {
    res.send(404, 'Page not found');
});

或类似的函数。

执行此操作而不是使用 app.get("/*", ...

A better solution would be to place the following code after all calls to app.use:

app.use(function(req, res) {
    res.send(404, 'Page not found');
});

Or a similar function.

Do this instead of using app.get("/*", ...

千鲤 2024-12-18 06:33:50

我的做法略有不同。如果您查看静态文件服务器的中间件代码,它允许调用一个出现错误的回调函数。唯一的问题是您需要响应对象将有用的内容发送回服务器。所以我执行以下操作:

var errMsgs = { "404": "Dang that file is missing" };
app.use(function(req, res, next){
    express.static.send(req, res, next, {
        root: __dirname + "/public",
        path: req.url,
        getOnly: true,
        callback: function(err) {
            console.log(err);
            var code = err.status || 404,
                msg = errMsgs["" + code] || "All is not right in the world";
            res.render("error", { code: code, msg: msg, layout: false});
        }
    });
});

基本上,如果出现错误,它会呈现我漂亮的错误页面并记录一些内容,以便我可以在某个地方进行调试。

I'm doing it a slightly different way. If you look at the middleware code for the static file server it allows for a callback function that gets called with errors. Only catch is you need the response object to send something useful back to the server. So I do the following:

var errMsgs = { "404": "Dang that file is missing" };
app.use(function(req, res, next){
    express.static.send(req, res, next, {
        root: __dirname + "/public",
        path: req.url,
        getOnly: true,
        callback: function(err) {
            console.log(err);
            var code = err.status || 404,
                msg = errMsgs["" + code] || "All is not right in the world";
            res.render("error", { code: code, msg: msg, layout: false});
        }
    });
});

Basically what happens is if there is an error it renders my pretty error page and logs something so that I can debug somewhere.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文