Reputation: 15034
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.
Upvotes: 5
Views: 16365
Reputation: 1
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.
Upvotes: 0
Reputation: 78
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 :)
Upvotes: 0
Reputation: 43
You can now, instead of using res.send(), use res.sendfile('index.html')
Upvotes: 1
Reputation: 23622
Using 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');
Upvotes: 9