I'm trying to serve some static file with a nicer url endpoints.
For example, /home
will serve /public/home.html
.
I can probably use res.sendfile()
in the route config, but sendfile will not cache the file output in production mode so I am not too sure if this is a good solution.
How do I set routes to act like an alias to a html file?
Delivering HTML files using Express can be useful when you need a solution for serving static pages.
In Express, route parameters are essentially variables derived from named sections of the URL. Express captures the value in the named section and stores it in the req. params property. You can define multiple route parameters in a URL.
Routing refers to determining how an application responds to a client request to a particular endpoint, which is a URI (or path) and a specific HTTP request method (GET, POST, and so on). Each route can have one or more handler functions, which are executed when the route is matched.
Try this.
var express = require('express');
var app =express.createServer();
// ------
app.get('/home', function(req,res){
res.sendfile(__dirname + '/public/home.html');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With