Reputation: 82
I'm working with node and express. I try to create a simple server using express.static. I have a file in the following folder on my server :
client/index.html
However, when I try this url : http://myServer.com/index.html, the server answers that :
Cannot GET /index.html
Here, you will find my used code :
var express = require('express');
var app = express();
app.use(express.static('client'));
/*
app.get('/', function (req, res) {
res.send('Hello World!');
});*/
var server = app.listen(8080, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
My file index.html is available. I already used other way to keep this like by using
app.get('/index.html', function (req, res, next) {
var options = {
root: __dirname + '/client/',
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
res.sendFile("index.html", options, function (err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
else {
console.log('Sent:', "index.html");
}
});
});
And this approach works.
Upvotes: 0
Views: 164
Reputation: 707218
You said that you were trying this URL:
http://myServer.com/index.html
But, your server is listening on port 8080, so you need to use this URL:
http://myServer.com:8080/index.html
or this:
Because express.static()
will automatically use index.html
for the /
path.
FYI, when I run your first block of code on my laptop with the proper URL, it works just fine. The browser shows me the contents of client/index.html where "client" is a sub-directory below where my app.js file is run from to start the server.
Upvotes: 1