I have a basic node.js app that I am trying to get off the ground using Express framework. I have a views
folder where I have an index.html
file. But I receive the following error when loading the web browser.
Error: Cannot find module 'html'
Below is my code.
var express = require('express');
var app = express.createServer();
app.use(express.staticProvider(__dirname + '/public'));
app.get('/', function(req, res) {
res.render('index.html');
});
app.listen(8080, '127.0.0.1')
What am I missing here?
For my project I have created this structure:
This code serves index.html for
/
requests, and reset.css for/css/reset.css
requests. Simple enough, and the best part is that it automatically adds cache headers.I added below 2 line and it work for me
Try res.sendFile() function in Express routes.
Read here : http://codeforgeek.com/2015/01/render-html-file-expressjs/
With Express 4.0.0, the only thing you have to do is comment out 2 lines in app.js:
And then drop your static file into the /public directory. Example: /public/index.html
I didn't want to depend on ejs for simply delivering an HTML file, so I simply wrote the tiny renderer myself:
To render Html page in node try the following,
You need to install
ejs
module throughnpm
like: