简体   繁体   English

从/提供静态文件

[英]Serve static file from /

If I define 如果我定义

app.use('/*', express.static(path.join(__dirname, '../pub/index.html')));

Then it appears that I need to provide at least one character after / in my url to get index.html. 然后看来,我需要在URL中的/之后至少提供一个字符以获取index.html。 If I define the route with just / , I get 404 for anything in the browser. 如果使用/定义路由,则浏览器中的任何内容都会得到404。

How do I get a default file served up with just / ? 如何使用/来提供默认文件?

You could use serve-static : 您可以使用serve-static

app.use(serveStatic(path.join(__dirname, '../pub'), {'index' ['index.html']}));

It should serve the index.html , which is located in ../pub , if you call eg localhost:3000 . 如果您调用例如localhost:3000 ,则它应提供位于../pub中index.html

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

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