3
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?

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
shaik
  • 35
  • 1
  • 9
  • 5
    This is how all web servers work. Even your OS works this way when you browse directories. What makes you think there's a magical way to figure out what the user wants without checking what the user types in? – slebetman May 18 '16 at 12:12
  • Whilst what you say is perfetly true @slebetman, i think OP - for some reason - thought they have to list every static file, rather than creating a file serving middleware. – Clemens Himmer May 18 '16 at 12:18
  • @ClemensHimmer: Ah. I saw the static middleware and thought he was asking why the static middleware needs to check the url each request. Because his question was "each time what the user is requesting for" rather than each path served. My bad. – slebetman May 19 '16 at 02:33

3 Answers3

5

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.

Paul
  • 35,689
  • 11
  • 93
  • 122
  • You're right, probably better to stick with basic stuff as the OP seems to have struggles undestanding the basics of web servers, +1 for simplicity. – Clemens Himmer May 19 '16 at 06:06
3

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();
            });
        });
    });
});
Clemens Himmer
  • 1,340
  • 2
  • 13
  • 26
  • Oh, absolutely not, if you can read comments and text posted with it, you should understand every bit of this answer. – Clemens Himmer May 18 '16 at 12:23
  • Also, i just added some more comments to the code, which is complementary to my post, - just for you. I'd be *really* surprised if a 11k user now can not see what this code does :) – Clemens Himmer May 18 '16 at 12:29
  • 2
    *I* can see what it does, but I'm not sure it's helping the OP to just give them a full solution to copy. – OrangeDog May 18 '16 at 12:51
1

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.

Node.js + Nginx - What now?

Community
  • 1
  • 1
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
  • 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? why do I need to check ? let say I am requesting for index.html file through browser url if the file exists it should or else it should throw me error. As I am new to node I am confused why do I need to check – shaik May 18 '16 at 13:30
  • "if the file exists" - that's called a check. What I think you are calling "checking" is actually defining a route, which the second part of my answer covers. – OrangeDog May 18 '16 at 13:40
  • Fundamentally, you cannot know that "index.html" is what you're looking for unless you check that. But as I said, I think you're just using the wrong word and confusing yourself. – OrangeDog May 18 '16 at 13:49