Any way to serve static html files from express without the extension?

前端 未结 4 2105
青春惊慌失措
青春惊慌失措 2020-12-03 07:45

I would like to serve an html file without specifying it\'s extension. Is there any way I can do this without defining a route? For instance instead of

 /he         


        
4条回答
  •  盖世英雄少女心
    2020-12-03 08:16

    While Robert's answer is more elegant there is another way to do this. I am adding this answer just for the sake of completeness. To serve static files without extension you can create a folder with the name of the route you want to serve against and then create an index.html file in it.

    Taking my own example if I wanted to serve hello.html at /hello. I would create a directory called hello and put an index.html file in it. Now when '/hello' is called express will automatically serve this file without the extension.

    Kind of obvious as this is supported by all web frameworks but I missed it back then.

提交回复
热议问题