Node.js在一个请求中提供多个文件 [英] Node.js serve multiple files in one request

查看:150
本文介绍了Node.js在一个请求中提供多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在一个请求中提供多个文件?

以下是我的代码的简化版本,用于在我们的node.js服务器不包含任何参数的请求中为我们网站的主页提供服务(有缓存代码用于缓存文件数据,使服务器更有效率,处理错误等):

  var http = require(http); 
$ b $ http.createServer(function(req,res){
res.setHeader(Access-Control-Allow-Origin,*);

if(req.url ==/){
res.writeHead(200,{Content-Type:text / html});

fs.readFile( ./pages/index/index.html,function(err,data){
res.end(data);
});
} else if(req.url == /index.css){
res.writeHead(200,{Content-Type:text / css});

fs.readFile(./ pages / index /index.css,函数(err,data){
res.end(data);
});
} else if(req.url ==/index.js ){
res.writeHead(200,{Content-Type:application / javascript});

fs.readFile(./ pages / index / index.js ,function(err,data){
res.end(data);
});
} else if(req.url ==/indexBackground.jpg){
res.writeHead(200,{Content-Type:image / jpeg});

fs.readFile(./ pages / index / images / ba ckground.jpg,函数(err,data){
res.end(data);
});
}

})。listen(port);

html文件本身使用CDN导入外部库,对样式表,脚本和图像的引用看起来像这:

 < link rel =stylesheethref =https://our-website-url/index.css > 
< script type =text / javascriptsrc =https://our-website-url/index.js>< / script>
< img src =https://our-website-url/indexBackground.jpg>

页面正确投放,但必须提供3个独立的请求才能投放页面。我想这会让网站加载速度变慢,并给服务器增加不必要的负载。

是否可以在一个请求中提供所有3个文件,尽管它们具有不同的内容类型?如果是的话,我们将如何引用这些文件,所有这些文件都在HTML文件中的一个请求中提供,如果没有,是否有更有效的方法来提供我们当前使用的页面?

解决方案

如果它是一个页面,您可以将它们放在脚本和样式标记中,图像可以放在数据url中,而且您很高兴。如果它是使用这些文件的多个页面,最好让浏览器获取并缓存它们。



我认为有一些协议(http2?),服务器可以发送浏览器接下来会请求的其他文件。但我不知道这项技术被称为什么。



祝你好运,有趣的问题!


Is it possible to serve multiple files in one request?

Here is a simplified version of my code to serve our website's home page on a request to our node.js server that contains no parameters (there is caching code that caches the file data to make the server more efficient, handles errors, etc.):

var http = require("http");

http.createServer(function(req, res){
    res.setHeader("Access-Control-Allow-Origin", "*");

    if (req.url == "/"){
        res.writeHead(200, {"Content-Type": "text/html"});

        fs.readFile("./pages/index/index.html", function(err, data){
            res.end(data);
        });
    } else if (req.url == "/index.css"){
        res.writeHead(200, {"Content-Type": "text/css"});

        fs.readFile("./pages/index/index.css", function(err, data){
            res.end(data);
        });
    } else if (req.url == "/index.js"){
        res.writeHead(200, {"Content-Type": "application/javascript"});

        fs.readFile("./pages/index/index.js", function(err, data){
            res.end(data);
        });
    } else if (req.url == "/indexBackground.jpg"){
        res.writeHead(200, {"Content-Type": "image/jpeg"});

        fs.readFile("./pages/index/images/background.jpg", function(err, data){
            res.end(data);
        });
    }

}).listen(port);

The html file itself imports external libraries using CDN's and the references to the stylesheet, script and image look like this:

<link rel="stylesheet" href="https://our-website-url/index.css">
<script type="text/javascript" src="https://our-website-url/index.js"></script>
<img src="https://our-website-url/indexBackground.jpg">

The page is served correctly but 3 separate requests must be made in order to serve the page. I would imagine this makes the website slower to load and adds unnecessary load to the server.

Is it possible to serve all 3 files in one request despite the fact they have different content types? If so how would we reference these files all served in one request in the html file, and if not, is there a more efficient method to serve the page that what we are currently using?

解决方案

If it's a single page, you can just put them in script and style tags, the images can be put in data-urls and you are happy. If it is multiple pages using those files, it is better to let the browser get and cache them.

I think there was some protocol (http2?) where the server can send additional files that the browser will request anyways next. But I have no idea what the technology was called.

Good luck, interesting question!

这篇关于Node.js在一个请求中提供多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆