5
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.

4 Answers 4

9

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');
Sign up to request clarification or add additional context in comments.

Comments

1

You can now, instead of using res.send(), use res.sendfile('index.html')

Comments

0

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 :)

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.