How can I make an html file in node.js

I tried to display the html file, but I got this error. I have login.html inside 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 view "login.html" view

 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) 
+1
javascript html render
source share
3 answers

Closest you can use html for node representations is EJS (built-in JavaScript)

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

http://embeddedjs.com/

https://github.com/visionmedia/ejs

+2
source share

Files stored in your shared folder will not be displayed because they are set as static, therefore they will be served as is. All other files outside this will be displayed. Do not forget to set the rendering mechanism.

0
source share

I just got in touch with the link today and wanted to solve the simillar problem by serving a simple HTML file. In my case, I wanted to show 404 when previous intermediaries did not complete the request.

I create a show404 function and then you can do something like app.use(show404) . Here, my CoffeScript for this basically inspired the errorHandler source code for the error.

 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 
0
source share

All Articles