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?
1) The best way is to set static folder. In your main file (app.js | server.js | ???):
public/css/form.html
public/css/style.css
Then you got static file from "public" folder:
2)
You can create your file cache.
Use method fs.readFileSync
I wanted to allow requests to "/" to be handled by an Express route where previously they had been handled by the statics middleware. This would allow me to render the regular version of index.html or a version that loaded concatenated + minified JS and CSS, depending on application settings. Inspired by Andrew Homeyer's answer, I decided to drag my HTML files - unmodified - into a views folder, configure Express like so
And created a route handler like so
This worked out pretty well.
Many of these answers are out of date.
Using express 3.0.0 and 3.1.0, the following works:
See the comments below for alternative syntax and caveats for express 3.4+:
Then you can do something like:
This assumes you have your views in the
views
subfolder, and that you have installed theejs
node module. If not, run the following on a Node console:try this. it works for me.
folder structure:
server.js
index.html
output:
hello world