`express.static()` keeps routing my files from the route

匆匆过客 提交于 2019-11-30 18:37:46

You must inverse the order of your router

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

means your router will be called first and, if no middleware handles the request, then express will call static files so, if you put the static middleware first, the express will handle static files first.

It is also recommended to put static middleware first.

For your problem you should try this:

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

Express will try static files first on public/html folder, then on the rest (including the public/html), i prefer putting html files on the root of public folder or maybe on a different folder (e.g public-html, static-html)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!