从URL获取文件扩展名,以便返回正确的内容类型,使用RegEx-良好做法

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

本文关键字:使用 类型 RegEx- 文件 获取 URL 扩展名 返回      更新时间:2023-09-26

我有两个问题:

No1:

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

例如

假设URL中的文件扩展名只能以以下格式出现:

CCD_ 1。

我得到的文件扩展名如下(请原谅我受C++启发的语法,这样读代码对我来说更容易/更干净):

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

我希望上述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.
}

因此,使用上面的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);
});

你认为以上方法有效吗?我只是想避免做一些类似"如果扩展名是js,那么使用这个内容类型,如果是css,那么使用this…等等"的事情然而,我确实在读取/循环一个包含646个扩展的JSON(针对每个请求),所以从性能的角度来看,我也不知道这是否明智。

我还想澄清一下,我不想在这个场景中使用Express.js(尽管可以举一个例子)。我试着把事情保持在尽可能低的水平,因为我是一个傻瓜,我想首先了解事情是如何运作的。

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

提前感谢您的帮助!:)

只需将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";

如果你需要从一个文件加载mimes,你可以把mimes保存在一个单独的json文件和ajax中,就像一样

mimes.json

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

如果URL类似于http://www.example.com/mywebsite/mypage怎么办?在这种情况下,问题中的regex和Patrick Evans的答案都会给出com/mywebsite/mypage作为文件扩展名。你关心这个案子吗?如果是这样,您可能需要单独处理它,例如,如果第一步的输出包含/,则将其更改为html