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


i beginner node.js , using express ejs layout, , want know how rid of .html extension when putting page. example if go localhost:3000/about.html - works want show /about. also, having trouble figuring out how change favicon if knows how change express default.

any great thanks.

(i realise question old, appears high in google search results, , accepted answer isn't best solution.)

the best solution serving static content in express.js express.static. avoid having specify file extensions in urls can configure list of default file extensions use when searching static files:

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

this serve pathtobasefolderofstaticcontent/somepage.html or pathtobasefolderofstaticcontent/somepage.htm in response request http://www.example.com/somepage, want. example, if visit https://arcade.ly/star-castle, file serves static file called star-castle.html. haven't had add special routing this, or other static file - it's handled express.static.

i need add specific routes content requires active work on server return. big advantage here can use cdn cache more of content (or nginx if running internal line of business app), reducing load on server.

you can configure many default file extensions like, although i'd tend keep list short. use resources url appear in address bar, means html files, although not always.

have @ following documentation on serving static content express.js:

this answered @ in express common way associate default file extension static content requests?.

the favicon.ico issue can solved dropping favicon root folder serve static content, implementing +costa's solution reference using <link> in <head> of documents.

in theory shouldn't need put favicon in root folder but, in practice, browsers still ask site root though it's referenced in <head> of document. leads spurious 404 error you'll able see in client side debugging tools (e.g., chrome dev tools).


Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -