node.js/express: Sending static files if they exist

前端 未结 2 1786
既然无缘
既然无缘 2021-01-13 17:17

I want to serve html files from a folder if they exist, otherwise fallback to the dynamic app.

currently I use something like:



        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-13 17:42

    You don't have to do anything special.

    i'm assuming the WebContent folder is in the root.

    And if all your static content are in the same base folder like you've shown, you don't have to specify it multiple times.

     app.use(express.static(__dirname + '/WebContent'));
    

    if you have a file called file.html in the WebContent folder you can now access it via the url i.e. localhost:4444/file.html

提交回复
热议问题