簡體   English   中英

如何在函數nodejs中傳遞參數值

[英]How to pass parameter value in function nodejs

創建函數后如何傳遞參數的值。 在這里,我想將path參數值傳遞給 for 循環。 請檢查我嘗試訪問該函數的第二個位置,然后傳遞參數的值。

我想從第一個函數創建一個模擬服務器客戶端,然后在特定路徑上調用后端。

從庫 mockServerClient 包中檢查下面。

/**
     * Start the client communicating at the specified host and port
     * for example:
     *
     *   var client = mockServerClient("localhost", 1080);
     *
     * @param host the host for the server to communicate with
     * @param port the port for the server to communicate with
     * @param path the path if server was deployed as a war
     */
    mockServerClient = function (host, port, path) {
        

        /**
         * The default headers added to to the mocked response when using mockSimpleResponse(...)
         */
        var defaultResponseHeaders = [
            {"name": "Content-Type", "values": ["application/json; charset=utf-8"]},
            {"name": "Cache-Control", "values": ["no-cache, no-store"]}
        ];
        var defaultRequestHeaders = [];

    

外部 for 循環

const client = mockServerClient('localhost', '8080');

inside for 循環在特定路徑上調用后端。

var array = [path1, path2, .....]; // array of some path

for (let i = 0; i < array.length; i++) {
  //here I want pass the path
   client.passValueOfParameter as path 
   this client will make some call backend call..
}

如果我理解正確,您想從第一個函數創建一個模擬服務器客戶端,然后在特定路徑上調用后端。

If I am wrong then please comment it out

一個簡單的“客戶”類就可以完成這項工作。

class Client {
    constructor(host, port) {
        this.host = host;
        this.port = port;
    }

    call(path) {
        // code to send the request
    }
}

您可以簡單地創建一個客戶端對象並調用“調用”函數來發送請求。

const paths = [path1, path2, ...path3];
const client = new Client('localhost', 8080);

// using 'path' package to join the paths
client.call(path.join(paths));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM