简体   繁体   English

NodeJS导出阻止代码

[英]NodeJS Exports Blocking Code

I am trying to create a parallel running server. 我正在尝试创建并行运行的服务器。 I have two main concerns: 我有两个主要问题:

  1. Some aspects are blocking and will stop another request being processed 某些方面正在阻止,并且将停止处理另一个请求
  2. Requests get mixed up and the wrong response goes to the wrong client. 请求混合在一起,错误的响应传递给错误的客户端。

Here is my code: 这是我的代码:

Server file: 服务器文件:

var http = require('http');
var controller = require('controller.js');
http.createServer(function (request, response) {
    controller.handle(request, response);
}).listen(8080, '0.0.0.0');

Controller file 控制器文件

var resp = require('./resp.js');
var sqltest = require('/sql/sqltest.js');

exports.handle = function(request, response) {
    request.on('data', function(chunk) {
        var json = JSON.parse(chunk);
        var type = parseInt(json["requestType"]);

        switch(type) {
            case 0:
                sqltest.try(json, function(res){
                resp.send(response, res);
                });
            break;
       }
     });
}

SQL Test File SQL测试文件

var mysql = require('mysql');
    exports.try = function(json, callback, res) {
    -- doo database stuff --
    callback(res);
    });
}

Response file 回应档案

exports.send = function(response, res){
    response.writeHead(200, ......);
    response.end(res);
}

Basically, this server is going to very large and I can't have tens of thousands of lines of code all sitting in one file. 基本上,该服务器将变得非常庞大,我无法将成千上万的代码行全部放在一个文件中。 By breaking it up, will I damage the performance in any way, will this cause any blocking code, will the server loose track of the correct client to respond to? 通过拆分它,我是否会以任何方式损害性能,是否会导致任何阻塞代码,服务器是否会松懈对正确客户端的响应?

I'm still very new with node and javascript in general so perhaps I'm miss-understanding how the basic concepts work. 总的来说,我对Node和javascript还是很陌生,所以也许我想念基本概念是如何工作的。 Any advice or improvements would be a great help! 任何建议或改进都会有很大帮助!

For concern #1, none of the code you have posted is blocking, but you're right to be concerned about this. 对于关注点1,您发布的代码都没有阻塞,但是您对此感到担心是正确的。 Node is single threaded and writing code that is either blocking or takes a long time to compute is a bad idea in node. Node是单线程的,因此编写阻塞的代码或花费很长时间进行计算的代码在Node中是个坏主意。 The good news is that node is very async oriented. 好消息是节点非常面向异步。 While its possible to write blocking code in node, the way common libraries are designed, you have to go out of your way to write a blocking function. 尽管可以在节点中编写阻塞代码(这是通用库的设计方式),但您必须竭尽全力编写阻塞函数。

This SO answer has a lot of good details to help wrap your head around it: https://stackoverflow.com/a/14797359/77501 这样的答案有很多很好的细节,可以帮助您绕开它: https : //stackoverflow.com/a/14797359/77501

For concern #2, I'm not sure what you mean. 对于问题2,我不确定您的意思。 Are you afraid that a SQL async callback will get invoked that that a res var is somehow pointing to someone else's request? 您担心SQL异步回调会被调用为res var以某种方式指向其他人的请求吗?

Regarding the blurb at the bottom, having code all of your in one file would have no perceptible impact on performance. 关于底部的内容,将所有代码包含在一个文件中不会对性能产生明显影响。 If anything your application might start up a tiny bit faster since it would have fewer modules to load. 如果有任何问题,您的应用程序可能会启动得更快,因为它将加载较少的模块。

Cramming tons of code into a single file is a bad idea for other reasons. 由于其他原因,将大量代码挤入一个文件是个坏主意。 Splitting your code up into modules will make your code more reusable, allowing you to write less of it. 将您的代码分成模块将使您的代码更具可重用性,从而使您可以编写更少的代码。 Less code is better. 代码越少越好。 Giving your thousands of lines of code a clear architecture by splitting it up into modules that all "do one thing well" (aka cohesion) will also make your code easier to understand, easier to build off of, and easier debug. 通过将成千上万的代码拆分为可以“一件事做好”(又称内聚性)的模块,从而为它们提供清晰的体系结构,这也将使您的代码更易于理解,更易于构建和调试。

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

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