简体   繁体   English

res.write需要很长时间才能加载

[英]res.write takes long time to load

May I know why with below code. 我可以用下面的代码知道为什么吗。 The browser takes a long time to load? 浏览器需要很长时间才能加载? I am new to express and node js. 我是表达和结点js的新手。 It takes almost 10s.If I was to replace res.write with console.log, the problem does not surface. 这几乎要花10秒钟的时间。如果我要用console.log替换res.write,问题就不会出现。

    app.get('/', function(req, res) {
    res.write("<!doctype html>");
    con.query('SELECT VoltageV from Voltage', function (err, rows, fields) {
        if (err) throw err
    var output = JSON.parse(JSON.stringify(rows));
            res.write("<SELECT>");
            res.write("</SELECT>");
    });

From Node.js api document Here it saids 从Node.js的API文档在这里它甾体抗炎药

If this method is called and response.writeHead() has not been called, it will switch to implicit header mode and flush the implicit headers. 如果此方法被称为和response.writeHead()没有被调用,将切换到隐式标头模式和冲洗隐式头。

This sends a chunk of the response body. 此发送响应主体的块。 This method may be called multiple times to provide successive parts of the body. 该方法可被调用多次,以提供身体的连续部分。

In short, your code is missing something that would affect performances, I think you will need to call res.writeHead() once and must be call before res.end() 总之,你的代码是失去了一些东西会影响演出,我想你会需要调用res.writeHead()一次,并且必须是之前调用res.end()

But if this does not help at all, the answer from this link question here might answer your question about how res.write performance. 但是,如果这并不能帮助所有,从这个答案的链接这里的问题可能回答有关如何res.write性能问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM