javascript - Node.js application not serving static webpage without root path -
i writing simple application in node.js, reason error "typeerror: path must absolute or specify root res.sendfile" when trying serve html page. error shouldn't take place because used express middleware specifies folder file found. why getting issue?
my app structure looks this
myapp public login.html app.js
my html page being served in app.js this
var express = require("express"); var app = express(); app.use(express.static("public")); app.get('/', function(req, res){ res.sendfile("login.html"); }); app.listen(3000);
i know around this, can "res.sendfile(__dirname + '/public/login.html');" instead shouldn't have since using middleware. ideas?
this line
app.use(express.static("public"));
does not think does!
it not set default folder every node function, in fact doesn't other node functions.
it says if webserver receives request, in folder first see if such file exists, , if so, serve file.
hence, it's route static files, used css , javascript files, , of course other static file doesn't serve dynamic content.
however, line
res.sendfile("login.html");
looks file login.html
in current working directory, or in other words you're node script running, not have @ static route, in fact correct way file regardless of script running,
res.sendfile(__dirname + '/public/login.html');
as correct path, required sendfile
find file.
Comments
Post a Comment