Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Express route alias to html file?

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?

like image 951
Teo Choong Ping Avatar asked Feb 05 '12 11:02

Teo Choong Ping


People also ask

Can Express serve HTML?

Delivering HTML files using Express can be useful when you need a solution for serving static pages.

What are Express route parameters?

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.

What is Express JS routing?

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.


1 Answers

Try this.

var express = require('express');
var app =express.createServer();
// ------
app.get('/home', function(req,res){
 res.sendfile(__dirname + '/public/home.html');
}); 
like image 87
Jude Calimbas Avatar answered Sep 29 '22 13:09

Jude Calimbas