0

I'm trying to understand some webserver code I found online and the part right after the "else if" is the only part I don't understand. (I just started learning this stuff). Thanks

var path = url.parse(req.url).pathname;

    // Managing the root route
    if (path == '/') {
        index = fs.readFile(__dirname+'/public/index.html',
            function(error,data) {

                //do stuff...
            });
    // Managing the route for the javascript files
    } else if( /\.(js)$/.test(path) ) {
        index = fs.readFile(__dirname+'/public'+path,
            function(error,data) {

                //do stuff...
            });
CollinA
  • 27

2 Answers2

2

It allows you to test against regular expresions. Heres the MDN on how it works.

In your example, it's checking if the path ends in .js

SpeedOfRound
  • 1,210
  • 11
  • 26
1

Regular expressions produce non strict match against the string. Besides MSDN @SpeedOfRound mentioned about it would be useful to play with this service to get the better understanding on your exact case