Render raw HTML


I want to render raw .html pages using Express 3 as follows:

server.get('/', function(req, res) {

This is how I have configured the server to render raw HTML pages (inspired from this outdated question):

    .set('view options', {layout: false})
    .set('views', './../')
    .engine('html', function(str, options) {
        return function(locals) {
             return str;

Unfortunately, with this configuration the page hangs and is never rendered properly. What have I done wrong? How can I render raw HTLM using Express 3 without fancy rendering engines such as Jade and EJS?

5/23/2017 12:02:50 PM

Accepted Answer

If you don't actually need to inject data into templates, the simplest solution in express is to use the static file server (express.static()).

However, if you still want to wire up the routes to the pages manually (eg your example mapping '/' to 'login.html'), you might try res.sendFile() to send your html docs over:

1/2/2013 3:27:00 AM

What I think you are trying to say is: How can I serve static html files, right?

Let's get down to it.

First, some code from my own project:

app.configure(function() {
    app.use(express.static(__dirname + '/public'));

What this means that there is a folder named public inside my app folder. All my static content such as css, js and even html pages lie here.

To actually send static html pages, just add this in your app file

app.get('/', function(req, res) {
  res.sendFile(__dirname + '/public/layout.html');

So if you have a domain called; whenever someone goes there, they will be served layout.html in their browsers.


If you are using express 4, things are a bit different. The routes and middleware are executed exactly in the same order they are placed.

One good technique is the place the static file serving code right after all the standard routes. Like this :

// All standard routes are above here'/posts', handler.POST.getPosts);

// Serve static files

This is very important as it potentially removes a bottleneck in your code. Take a look at this stackoverflow answer(the first one where he talks about optimization)

The other major change for express 4.0 is that you don't need to use app.configure()

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow