javascript - Error when rendering HTML in express -


i'm trying out simple 1 page render , i'm doing wrong:

var express = require('express'); var app = express();  app.get('/', function(req, res){    res.render('home.html'); });  app.listen(3000); console.log('listening on port 3000'); 

error:

error: cannot find module 'html' 

i want load page css/images , wanted know store css/images/scripts/html , how load it. have yet find many example express apps.

when using res.render, rendering defined view. documentation state:

render view callback responding rendered string. when error occurs next(err) invoked internally.

for specific html, either have load file fs.readfile, or have express use rendering engine, such jade or ejs. example, code sample apply ejs render engine files extension .html.

app.engine('html', require('ejs').renderfile); 

then can render file this. example of passing variables html file, because that's embedded javascript used for.

app.get('/', function (req, res) {   res.render('/file', {     pass: 'arguments',     to: 'the file here'   }); }); 

that more convenient having read file on each load so:

app.get('/', function (req, res) {   fs.readfile('/index.html', 'utf8', function (err, data) {     res.send(data);   }); }); 

although aside question, how passing variables ejs works. works if statements , loops, since accepts javascript. putting console.log in file log each time file rendered.

<span><%= pass %></span> <!-- after rendering --> <span>arguments</span> 

if need serve other resources, such css, javascript, or other html files, use express' directory function.

app.use(express.static('/')); 

then wouldn't need render html @ all. statically served.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -