Reputation: 35
var express = require("express");
var app = express();
var path = require("path");
app.use(express.static(__dirname + '/view'));
app.get('/dashboard',function(req,res){
res.sendFile((path.join(__dirname + '/dashboard.html'));
});
app.get('/index1',function(req,res){
res.sendFile((path.join(__dirname+'/index.html'));
});
app.get('/',function(req,res){
res.redirect('/login');
});
app.get('/login',function(req,res){
res.redirect((path.join(__dirname + '/login'));
});
app.listen(3000);
console.log("Running at Port 3000");
My Problem here is why do I need to check each time what user is requesting for?
Also, what if I have 100 html files in my directory do I need to check the each file through get method and then return the page through res.sendFile
?
Upvotes: 3
Views: 2741
Reputation: 36349
What @OrangeDog and @Clemens Himmer said are both true. However, the simplest way to serve all the files in a directory is already in your script:
app.use(express.static(__dirname + '/view'));
That will serve everything in your view
directory if the name matches the URL, e.g. http://yoursite.com/index.html will resolve to the file at __dirname + '/view/index.html'.
In your example routes, however, you seem to be changing the URL path to no longer match the file location (for example, /login
you want to resolve to '/login.html'). You could definitely write middleware to munge that and resolve the file based on a pattern, but at that point it's far simpler to use a purpose built web server (like nginx as previously suggested) which has URL rewrite features already baked in.
Upvotes: 5
Reputation: 1352
No, you don't have to list all your static files programatically. You can serve them dynamically by binding middleware to the server's root.
This is a small express.js
script i've done, it's basically a really simple web server that serves anything and pretty HTML.
// This servers a file..
var serveFile = function(filePath, res){
var options = {
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
res.sendFile(filePath, options, function (err) {
if (err) {
console.log(err);
res.status(err.status).end();
}
});
};
// Serve web files
app.use("/", function (req, res, next) {
var filePath = (absoluteServePath + req.originalUrl).replace(/\//g,"\\");
var checkFilePath = function(filePath){
return new Promise(function(resolve, reject) {
fs.access(filePath, fs.F_OK, function(err) {
if(!err){
// If FILE / DIR exists check if file or DIR
if(fs.lstatSync(filePath).isDirectory() == true){
reject();
}
else{
resolve();
}
}else{
reject(err);
}
});
});
};
checkFilePath(filePath).then(function(){
serveFile(filePath,res);
},function(){
// Check if path ends with a slash
var endsWithSlash = filePath.substr(filePath.length - 1) == "\\";
// Check if a index.html exists in the path
var indexHTMLPath = filePath + ((endsWithSlash == true) ? "" : "\\") + "index.html";
checkFilePath(indexHTMLPath).then(function(){
serveFile(indexHTMLPath,res);
},function(){
// Check if .html for the path exists
var plusHTMLPath = filePath +".html";
checkFilePath(plusHTMLPath).then(function(){
serveFile(plusHTMLPath,res);
},function(){
// Nope, does not exist at all
next();
});
});
});
});
Upvotes: 3
Reputation: 38836
My Problem here is why do I need to check each time what user is requesting for?
How are you supposed to give the user what they requested if you don't check what that is?
What if I have 100 html files in my directory do I need to check the each file through get method and then return the page through res.sendFile?
If you mean do you need to declare a separate route for every file, then the answer is no. Express routes can be pattern-based, so you can define a route for e.g. a whole directory, then return the specific file that was requested: Simple static HTML server in Node.
This all leads on however to Node.JS not being a great choice for serving lots of static content. There are many security and other concerns you need to take care of, and it's just not as performant as it could be. You are likely to have a better time if you use an nginx or apache2 server to serve these files, forwarding dynamic requests to your Node server.
Upvotes: 1