当前位置: 动力学知识库 > 问答 > 编程问答 >

How to get rid of .html extension when serving webpages with node.js?

问题描述:

I am a beginner with node.js and am using express with the ejs layout, and I want to know how to get rid of the .html extension when putting up a page. For example if I go to my localhost:3000/about.html - that works but I want it to show up as just /about. Also, having trouble figuring out how to change the favicon if anyone knows how to quickly change that from the express default.

Any help would be great thanks.

网友答案:

(I realise this question is old, but it appears high in Google search results, and the accepted answer isn't the best solution.)

The best solution for serving up static content in express.js is express.static. To avoid having to specify file extensions in URLs you can configure it with a list of default file extensions that it will use when searching for static files:

app.use(express.static(pathToBaseFolderOfStaticContent, {
    extensions: ['html', 'htm'],
    ... // Other options here
}));

This will serve up pathToBaseFolderOfStaticContent/somePage.html or pathToBaseFolderOfStaticContent/somePage.htm in response to a GET request to http://www.example.com/somePage, which is what you want. For example, if you visit https://arcade.ly/star-castle, the file it serves up is just a static file called star-castle.html. I haven't had to add any special routing for this, or any other static file - it's all just handled by express.static.

I only need to add specific routes for content that requires active work on the server to return. A big advantage here is that I can use a CDN to cache more of my content (or nginx if I were running an internal line of business app), thus reducing load on my server.

You can obviously configure as many default file extensions as you like, although I'd tend to keep the list short. I only use it for resources where the URL is likely to appear in the address bar, which generally means HTML files, although not always.

Have a look at the following documentation on serving static content with express.js:

  • http://expressjs.com/en/starter/static-files.html
  • http://expressjs.com/en/4x/api.html (the express.static documentation is at the top)

This is also answered at In express what is the common way to associate a default file extension with static content requests?.

The favicon.ico issue can be solved by dropping your favicon into the root folder from which you serve static content, as well as implementing +Costa's solution where you reference it using a <link> in the <head> of your documents.

In theory you shouldn't need to do put the favicon in the root folder but, in practice, some browsers will still ask for it from the site root even though it's referenced in the <head> of your document. This leads to a spurious 404 error that you'll be able to see in client side debugging tools (e.g., Chrome dev tools).

网友答案:

The Favicon issue is usually a caching problem. As long as you have this code in your base html layout:

<link rel="shortcut icon" type="image/x-icon" href="/images/favicon.ico">

Then just navigate to wherever that image is with your browser, and that should force your cache to update.

网友答案:

I figured it out. I looked at this post Render basic HTML view in Node JS Express? which solved the problem I was having.

app.engine('html', require('ejs').renderFile);    

app.get('/', function(req, res){
  res.render("index.html");
});

And this all goes in the app.js or whatever file you are running.

分享给朋友:
您可能感兴趣的文章:
随机阅读: