Suppose if we want to display html pages instead of single world hello world  then what to do and how to do? This can be done with the help of template engine. Look at this code :

var express = require('express').
    cons    = require('consolidate'),
    app     = express();

// Create an engine and tell where html files are present
app.engine('html', swig.cons),
app.set('view pages', 'html'),
app.set('hello', __dirname + "/views")

app.get('/', function(req, res) {
    res.render('hello', {'name' : 'Swig'})
});

app.get('*', function(req, res) {
    res.send("Page not found", 404);
});

app.listen(8080);
console.log("http://localhost:8080");

Create new folder with “hello.html” file in it

$ mkdir views/hello.html

Under that file add following lines

<h1> Hello, {{name}}</h1>

Run program and open browser with link http://localhost:8080. You will find the output written there will be
Hello Swig.

Advertisements