node.js - Node express static files -
i'm working node , express. try create simple server using express.static. have file in following folder on server :
client/index.html
however, when try url : http://myserver.com/index.html, server answers :
cannot /index.html
here, find 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 @ http://%s:%s', host, port); });
my file index.html available. used other way keep 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 approach works.
you said trying url:
http://myserver.com/index.html
but, server listening on port 8080, need use url:
http://myserver.com:8080/index.html
or this:
because express.static()
automatically use index.html
/
path.
fyi, when run first block of code on laptop proper url, works fine. browser shows me contents of client/index.html "client" sub-directory below app.js file run start server.
Comments
Post a Comment