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?
I was trying to set up an angular app with an express RESTful API and landed on this page multiple times though it wasn't helpful. Here's what I found that worked:
Then in the callback for your api routes look like:
res.jsonp(users);
Your client side framework can handle routing. Express is for serving the API.
My home route looks like this:
I also faced the same issue in
express 3.X
andnode 0.6.16
. The above given solution will not work for latest versionexpress 3.x
. They removed theapp.register
method and addedapp.engine
method. If you tried the above solution you may end up with the following error.To get rid of the error message. Add the following line to your
app.configure function
Note: you have to install
ejs
template engineExample:
Note: The simplest solution is to use ejs template as view engine. There you can write raw HTML in *.ejs view files.
if you are using express framework to node.js
install npm ejs
then add config file
;
render the page from exports module form.js have the html file in the views dir with extension of ejs file name as
form.html.ejs
then create the form.js
res.render('form.html.ejs');
In server.js, please include