Grab file extension from URL, in order to return correct Content-Type, using RegEx - Good practice?

I have 2 questions:

No1 :

I'm trying to come up with a RegEx that will return the file extension found at the end of the URL :

e.g.

Assuming that a file extension in a URL can only appear in this format :

.../.../..../..../filename.fileextension.

I'm getting the file extension as follows (please excuse my C++ inspired syntax, it's just much easier/cleaner for me to read code like that):

var fileExtension;
if (document.location.href.match(/.*\.(.*)$/) && document.location.href.match(/.*\.(.*)$/)[1])
{
  fileExtension = document.location.href.match(/.*\.(.*)$/)[1];
}

What I hope the above RegEx means is match everything after that last dot in the URL.

Is that the case or is there a case where the above RegEx will fail? And if so, what would the correct RegEx be?

No2 :

The reason why I want to do the above is because I want to set up a my first Node.js server and I'm trying to make it as efficient as possible. Whenever a request is received for a specific file, I want to loop through an array of all possible file extensions, find the right one, and return the appropriate content-type in the head of my response. For this reason, I have created a JSON file (content-types.json) containing all possible file extensions (646 to be exact..), along with the relevant content types. It looks something like this :

{
  "3dm"       : "x-world/x-3dmf",
  "3dmf"      : "x-world/x-3dmf",
  "a"         : "application/octet-stream",
  "aab"       : "application/x-authorware-bin",
  "aam"       : "application/x-authorware-map",
   ......etc.
}

So using the above JSON file, I'm creating my server.js like this :

var http = require('http');
var fs = require('fs');

var contentTypes = JSON.parse(fs.readFileSync("content-types.json"));

var host = "127.0.01";
var port = 1825;

var server = http.createServer(function (request, response)
{
  fs.readFile("." + request.url, function (error, data)
  {
    if (error)
    {
      response.writeHead(404, {"Content-type" : "text/plain"});
      response.end("something went wrong");
    }

    else if (request.url.match(/.*\.(.*)$/) && request.url.match(/.*\.(.*)$/)[1])
    {
      var extension = request.url.match(/.*\.(.*)$/)[1];
      var contentType;
      var found = false;

      // now I'll loop through the content-types object and look for a match
      // if a match is found then 
      // found = true; 
      // contentType = contentTypes[extension];

      if (!found)
      {
        response.writeHead(404, {"Content-type" : "text/plain"});
        response.end("wrong file extension");
      }

      else
      {
        response.writeHead(200, {"Content-type" : contentType});
        response.end(data);
      }
    }

    else
    {
      response.writeHead(404, {"Content-type" : "text/plain"});
      response.end("file extension not recognised");
    }
  });
});

server.listen(port, host, function ()
{
  console.log("listening " + host + ":" + port);
});

Do you think the above is efficient? I'm simply trying to avoid doing something like "if extension is js then use this content type, if it's css then this..etc." However, it's true that's I'm reading/looping through a JSON (for every request) containing 646 extensions, so I don't know if that's wise either, from a performance perspective.

I also want to clarify that I don't want to use Express.js in this scenario (although an example would be appreciated). I'm trying to keep things as low-level as possible, because I'm a noob and I want to understand how stuff works first.

Also, as you can tell I haven't written the proper loop that will look for the right content-type because basically I don't know how to loop through an object. Should I use an array of objects instead? But in that case, I wouldn't be able to save that as an external file that I would simply read right? It has to be a JSON file I believe. So how do I loop through that contentTypes object in the above scenario in order to grab the appropriate contentType based on a given extension-key?

Thank you in advance for any help! :)

Just put the mimes into a "associative array" (actually just a object with named properties being the extensions) and then use the extension as the key/index

mimes = {
   "js":"text/javascript",
   "css":"text/css",
   "json":"application/json"
};
var ext = request.url.split(".").pop();

//Check if the extension mime association exists, if not default to text/html
if( typeof(mimes[ext]) != "undefined" )
   mime = mimes[ext];
else
   mime = "text/html";

if you need to load the mimes from a file you could just save the mimes in a seperate json file and ajax it like:

mimes.json

{
   "js":"text/javascript",
   "css":"text/css",
   "json":"application/json"
}

What if the URL is something like http://www.example.com/mywebsite/mypage? In that case, both the regex in the question and Patrick Evans' answer would give com/mywebsite/mypage as the file extension. Do you care about this case? If so, you'll probably ave to treat it separately, e.g. if the output of the first step contains a / then change it to html.