【问题标题】:Is it possible to continuously add in a loop? [duplicate]是否可以在循环中连续添加? [复制]
【发布时间】:2020-11-24 14:36:00
【问题描述】:

我正在使用节点模块来创建 http 代理服务器(node-socks),如下所示:

const { http } = require('@sansamour/node-socks')




http.createServer({
    authorization: function(u,p){
        return u == 'KeemROH' && p == 'Test'
    },
    port: 5000
});

http.createServer({
    authorization: function(u,p){
        return u == 'KeemROH' && p == 'Test'
    },
    port: 5001
});

http.createServer({
    authorization: function(u,p){
        return u == 'KeemROH' && p == 'Test'
    },
    port: 5002
});

http.createServer({
    authorization: function(u,p){
        return u == 'KeemROH' && p == 'Test'
    },
    port: 5003
});

http.createServer({
    authorization: function(u,p){
        return u == 'KeemROH' && p == 'Test'
    },
    port: 5004
});

是否有可能制作一个可以运行的循环:

http.createServer({
    authorization: function(u,p){
        return u == 'KeemROH' && p == 'Test'
    },
    port: 5000 // add +1 each loop so 5000, 5001, 5002, 5003, 5004 ect.
});

让它循环一定次数? 因此,与其重写多次,这可能是解决方案

【问题讨论】:

  • let port = 5000; http.createServer({..., port: port++}); ?
  • @HereticMonkey 不幸的是这不起作用
  • 当然,对于您发布的代码,循环应该是微不足道的。请向我们展示您的尝试。
  • 不清楚您所说的“持续”是什么意思。您的循环需要在某个时候停止(在您的示例中为 5005)。另外,你为什么要创建多个代理服务器?
  • @Bergi 在我的情况下,这个数字可能被配置为在我的应用程序中允许一定数量的端口

标签: javascript node.js loops http proxy


【解决方案1】:

这是你要找的东西吗:

const { http } = require('@sansamour/node-socks')

const numServers = 5;
const startingPort = 5000;

for (let i = 0; i < numServers; i++) {
    http.createServer({
        authorization: function(u,p){
            return u == 'KeemROH' && p == 'Test'
        },
        port: startingPort + i
    });
}

【讨论】:

    猜你喜欢
    • 2020-09-21
    • 2019-02-10
    • 1970-01-01
    • 2020-09-07
    • 2021-03-10
    • 2011-08-19
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    相关资源
    最近更新 更多