Not Found when serving express static file

Tags: , ,



I would like to access to a page of my web application. I created the server with expressJs and I serve the route by using 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.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')); work fine : when I go to http://localhost:8080/MPS the page index.html is displayed.

But when I go to http://localhost:8080/Consultation or http://localhost:8080/Parametrage, none of consultation.html or parametrage.html is displayed.

I have this error : Cannot GET /Consultation/

I don’t know how to fix it so any help would be greatly appreciated.

Answer

If you don’t specify a specific html-page in your url the default is index.html. Since there are no index files in Consultation/Parametrage subfolders you see above error.

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.



Source: stackoverflow