var express = require("express");
var fs = require('fs');
var sys = require('sys');
var app = express();
app.use(express.logger());
app.get('/', function(req, res){
fs.readFile('/views/index.html');
});
app.listen(8080);
console.log('Express server started');
I don't want to use the template engine jade. How can i open a simple index.html page which resides inside my view folder. The server is getting started, but it seems i am not able to load the index.html page.
var express = require("express");
var fs = require('fs');
var sys = require('sys');
var app = express();
app.use(express.logger());
app.get('/', function(req, res){
fs.readFile('/views/index.html');
});
app.listen(8080);
console.log('Express server started');
I don't want to use the template engine jade. How can i open a simple index.html page which resides inside my view folder. The server is getting started, but it seems i am not able to load the index.html page.
Share Improve this question asked Dec 7, 2012 at 14:44 theJavatheJava 15k48 gold badges134 silver badges174 bronze badges4 Answers
Reset to default 9Using express 3.0.0rc3, the following works:
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
Or
app.set("view options", {layout: false});
app.use(express.static(__dirname + '/public'));
So your final code would look like this.
var express = require("express");
var fs = require('fs');
var sys = require('sys');
var app = express();
app.use(express.logger());
app.set("view options", {layout: false});
app.use(express.static(__dirname + '/views'));
app.get('/', function(req, res){
res.render('/views/index.html');
});
app.listen(8080);
console.log('Express server started');
You can now, instead of using res.send(), use res.sendfile('index.html')
Using express v4.7.1 and without using the template engine jade, this works fine.
In the latest releases, the sendfile() has been changed to sendFile().
For a file path, the server tries to find the path of the file from the root directory, so it gets mandatory to specify either the entire path or use the '_dirname', else always the file not found error will be faced.
var express = require("express");
var path = require('path');
var app = express();
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/views/index.html'))
});
app.listen(8080);
console.log('Express server started');
Hope it helps. Happy Coding :)
let express=require('express');
let app=express();
app.get( '/', (req,res)=>{
res.sendFile(__ dirname + "/my.txt");
})
app.listen(4000);
Try this. It will work fine.