简体   繁体   English

提供快速静态文件时未找到

[英]Not Found when serving express static file

I would like to access to a page of my web application.我想访问我的 Web 应用程序的页面。 I created the server with expressJs and I serve the route by using app.use(express.static()) .我使用 expressJs 创建了服务器,并使用app.use(express.static())为路由提供服务。

Here is my tree :这是我的树:

client
    |___Consultation
    |    |___Parametrage
    |    |    |
    |    |    |___parametrage.html
    |    |    |___parametrage.css
    |    |    |___parametrage.js
    |    |
    |    |___consultation.html
    |
    |___index.html
app.js

In the app.js file, I have this :app.js文件中,我有这个:

app.use('/MPS', express.static(__dirname + '/client'));
app.use("/Consultation", express.static(__dirname + '/client/Consultation'));
app.use("/Parametrage/", express.static(__dirname + '/client/Consultation/Parametrage/'));

The line app.use('/MPS', express.static(__dirname + '/client'));app.use('/MPS', express.static(__dirname + '/client')); work fine : when I go to http://localhost:8080/MPS the page index.html is displayed.工作正常:当我访问http://localhost:8080/MPS ,会显示页面index.html

But when I go to http://localhost:8080/Consultation or http://localhost:8080/Parametrage , none of consultation.html or parametrage.html is displayed.但是当我访问http://localhost:8080/Consultationhttp://localhost:8080/Parametrage ,并没有显示consultation.htmlparametrage.html

I have this error : Cannot GET /Consultation/我有这个错误: Cannot GET /Consultation/

I don't know how to fix it so any help would be greatly appreciated.我不知道如何解决它,所以任何帮助将不胜感激。

If you don't specify a specific html -page in your url the default is index.html .如果您没有在 url 中指定特定的html -page,则默认值为index.html Since there are no index files in Consultation/Parametrage subfolders you see above error.由于 Consultation/Parametrage 子文件夹中没有索引文件,因此您会看到上述错误。

If you request it with如果你请求它

http://localhost:8080/Consultation/consultation.html

it should work fine.它应该可以正常工作。 Alternatively you can rename consultation.html to index.html as well and request it with http://localhost:8080/Consultation .或者,您也可以将consultation.html index.html重命名为index.html并使用http://localhost:8080/Consultation请求它。

Your location /client has the file index.html Try putting the same file in other locations.您的位置/client有文件index.html尝试将相同的文件放在其他位置。 Basically rename consultation.html -> index.html and parametrage.html -> index.html基本上重命名consultation.html -> index.htmlparametrage.html -> index.html

Go with the following syntax for other routes对其他路由使用以下语法

app.use('/admin', function (req, res, next) {
  // GET 'http://www.example.com/admin/new'
  console.dir(req.originalUrl)
  // '/admin/new' console.dir(req.baseUrl)
  // '/admin' console.dir(req.path) 
 // '/new' next() 
});

or或者

locate index file http://localhost:8080/Consultation/consultation.html找到索引文件http://localhost:8080/Consultation/consultation.html

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

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