从URL获取文件扩展名,以便使用RegEx返回正确的Content-Type - 良好做法?

Spr*_*der 0 javascript regex json content-type node.js

我有两个问题:

No1:

我正在尝试提供一个RegEx,它将返回在URL末尾找到的文件扩展名:

例如

假设URL中的文件扩展名只能以这种格式显示:

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

我正在获得如下文件扩展名(请原谅我的C++启发语法,这对我来说更容易/更清晰地阅读这样的代码):

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

我希望上面的RegEx意味着匹配URL中最后一个点之后的所有内容.

是这种情况还是存在上述RegEx失败的情况?如果是这样,正确的RegEx会是什么?

No2:

我想要做到这一点的原因是因为我想建立一个我的第一个Node.js服务器,我试图让它尽可能高效.每当收到特定文件的请求时,我想循环遍历所有可能的文件扩展名的数组,找到正确的文件扩展名,并在我的响应的头部返回相应的内容类型.出于这个原因,我创建了一个JSON文件(content-types.json),其中包含所有可能的文件扩展名(准确地说是646),以及相关的内容类型.它看起来像这样:

{
  "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.
}
Run Code Online (Sandbox Code Playgroud)

所以使用上面的JSON文件,我正在创建我的server.js,如下所示:

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);
});
Run Code Online (Sandbox Code Playgroud)

你认为以上是否有效?我只是试图避免做类似"如果扩展是js然后使用这种内容类型,如果它是css然后这个..等." 但是,我正在读取/循环包含646个扩展的JSON(对于每个请求),所以从性能角度来看,我也不知道这是否明智.

我还想澄清一下,我不想在这种情况下使用Express.js(尽管我会赞赏一个例子).我试图让事情尽可能低,因为我是一个菜鸟,我想先了解它是如何工作的.

另外,正如你所知道的那样,我没有编写适当的循环来寻找正确的内容类型,因为基本上我不知道如何遍历一个对象.我应该使用一组对象吗?但是在那种情况下,我无法将其保存为外部文件,我只是阅读了吗?它必须是我认为的JSON文件.那么如何在上面的场景中循环遍历该contentTypes对象,以便根据给定的扩展键获取适当的contentType?

预先感谢您的任何帮助!:)

Pat*_*ans 6

只需将mimes放入"关联数组"(实际上只是一个命名属性为扩展名的对象),然后使用扩展名作为键/索引

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";
Run Code Online (Sandbox Code Playgroud)

如果你需要从一个文件中加载mimes,你可以将mimes保存在一个单独的json文件中,并且ajax就像:

mimes.json

{
   "js":"text/javascript",
   "css":"text/css",
   "json":"application/json"
}
Run Code Online (Sandbox Code Playgroud)