简体   繁体   English

Express JS错误404页面拦截了静态文件

[英]Express js error 404 page intercepts with static files

I used the code below to handle error 404 but it is intercepting with my static files. 我使用下面的代码来处理错误404,但是它正在拦截我的静态文件。 Here are my static file structure 这是我的静态文件结构

-public
   -images [folder]
    -uploaded [folder]
     -image.png

app.use(express.static('public'));

app.use(function(req, res) {
        res.status(400);
        res.sendFile(path.join(__dirname + '/views/404.html'));
});

app.get("/images", (req, res) => {
    var path = "public/images/uploaded/";
    var images = "";
    fs.readdir(path, function(err, items) {
        images = items;
        res.json({ images });
    });
});

The '/images' route returns error 404. How can I correct this? “ / images”路由返回错误404。如何更正此错误?

Move the error handler function to be the bottom. 将错误处理程序功能移到底部。 ie it should come after all the other route definitions. 即它应该排在所有其他路由定义之后。

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

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