This is what i have, the filename "default.htm" actually exists and loads when doing a readFile with NodeJS.
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/default.htm'));
app.listen(process.env.PORT);
The Error (in browser):
Cannot GET /
You typically want to render templates like this:
app.get('/', function(req, res){
res.render('index.ejs');
});
However you can also deliver static content - to do so use:
app.use(express.static(__dirname + '/public'));
Now everything in the /public
directory of your project will be delivered as static content at the root of your site e.g. if you place default.htm
in the public folder if will be available by visiting /default.htm
Take a look through the express API and Connect Static middleware docs for more info.
You need to define a root route.
app.get('/', function(req, res) {
// do something here.
});
Oh and you cannot specify a file within the express.static
. It needs to be a directory. The app.get('/'....
will be responsible to render that file accordingly. You can use express' render method, but your going to have to add some configuration options that will tell express where your views are, traditionally within the app/views/
folder.
Where is your get method for "/"?
Also you cant serve static html directly in Express.First you need to configure it.
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set("view options", {layout: false}); //This one does the trick for rendering static html
app.engine('html', require('ejs').renderFile);
app.use(app.router);
});
Now add your get method.
app.get('/', function(req, res) {
res.render('default.htm');
});