silvesterprabu
silvesterprabu

Reputation: 1457

how can i render html file in node.js

i have tried to render html file but i got this error . i have login.html within public folder.how to render html file.thanks in advance.

my server side coding

var express = require('express');

var app = express();

app.configure(function(){

  app.set("view options", {layout: false});

  app.use(express.static(__dirname + '/public'));


 });

app.get('/', function(req, res) {

    res.render('login.html');

});

app.listen(8000)

Error: Failed to lookup view "login.html"

at Function.render (/home/aware/local/src/node_modules/express/lib/application.js:493:17)
at ServerResponse.render (/home/aware/local/src/node_modules/express/lib/response.js:753:7)
at /home/aware/local/src/health/demo2.js:17:9
at callbacks (/home/aware/local/src/node_modules/express/lib/router/index.js:161:37)
at param (/home/aware/local/src/node_modules/express/lib/router/index.js:135:11)
at pass (/home/aware/local/src/node_modules/express/lib/router/index.js:142:5)
at Router._dispatch (/home/aware/local/src/node_modules/express/lib/router/index.js:170:5)
at Object.router [as handle] (/home/aware/local/src/node_modules/express/lib/router/index.js:33:10)
at next (/home/aware/local/src/node_modules/express/node_modules/connect/lib/proto.js:199:15)
at resume (/home/aware/local/src/node_modules/express/node_modules/connect/lib/middleware/static.js:60:7)

Upvotes: 1

Views: 4504

Answers (4)

MD OZAIR QAYAM
MD OZAIR QAYAM

Reputation: 11

You can render HTML page with Nodejs using Embedded JavaScript(ejs). here you can go through -the Documentation of ejs

for using ejs, first of you create directory with the name views, In that directory create a file with .ejs extension.

Upvotes: 1

David Pelaez
David Pelaez

Reputation: 1384

I just came in contact with connect today & wanted to solve a simillar issue, serving a simple HTML file. In my case, I wanted to display a 404 when any previous middlewares hadn't finished the request.

I create a function, show404 and then you can do something like app.use(show404). Here's my CoffeScript for that mostly inspired in connect errorHandler source code.

show404 = (req, res, next) ->
    res.statusCode = 404
    res.setHeader 'Content-Type', 'text/html; charset=utf-8'
    html_path = require("path").resolve('.tmp/404.html')
    require('fs').readFile html_path, (err, data)->
        throw err if err
        console.log "DATA:", data
        res.end data

Upvotes: 0

ExxKA
ExxKA

Reputation: 1030

The files you keep inside your public folder will not be rendered because they are set as static, so they will be served as is. All other files outside of this will be rendered. Remember to set the rendering engine.

Upvotes: 0

ashley
ashley

Reputation: 2767

The closest you can get to using html for node views is EJS (Embedded JavaScript)

This way you write your normal HTML, pass your data through node and display them using tags like: <%= var %>

http://embeddedjs.com/

https://github.com/visionmedia/ejs

Upvotes: 2

Related Questions