是否可以在循环中不断添加?

Is it possible to continuously add in a loop?

我正在使用节点模块创建 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.
});

让它循环一定次数? 因此,与其重写多次,不如这样解决

这是您要找的吗:

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
    });
}