路由文件中的函数调用-node.js

时间:2019-01-28 04:57:31

标签: node.js

以下代码不起作用,因为有函数调用。

function checkQueryStringExists(request) {
    var url_parts = url.parse(request.url, true);
    var api_token = url_parts.query.api_token;
    if(typeof api_token === 'undefined') {
        return false;
    }
    return true;
}

router.post("/get-profile", function(request, response) {
    response.setHeader('Content-Type', 'application/json');

    var result = checkQueryStringExists(request);
    if(!result) {
        response.status(401).send(JSON.stringify({ "Status": false, "Message": "Invalid query string." }));
    }
});

以下代码有效,因为函数调用已被删除

router.post("/get-profile", function(request, response) {
    response.setHeader('Content-Type', 'application/json');

    var url_parts = url.parse(request.url, true);
    var api_token = url_parts.query.api_token;
    if(typeof api_token === 'undefined') {
        response.status(401).send(JSON.stringify({ "Status": false, "Message": "Invalid query string." }));
    }

});

这是否意味着我们不能在上述的node.js中使用函数?

3 个答案:

答案 0 :(得分:0)

在节点js中,您可以在不指定function关键字的情况下定义函数。您可以通过两种方法来定义函数:

方法1

checkQueryStringExists(request) {
    var url_parts = url.parse(request.url, true);
    var api_token = url_parts.query.api_token;
    if(typeof api_token === 'undefined') {
        return false;
    }
    return true;
}

APPROACH 2

checkQueryStringExists = (request) => {
    var url_parts = url.parse(request.url, true);
    var api_token = url_parts.query.api_token;
    if(typeof api_token === 'undefined') {
        return false;
    }
    return true;
}

转机

答案 1 :(得分:0)

您绝对可以在NodeJS中使用函数。尽管最好的使用函数的方法是使用async / await,这样您可以确保在执行代码之前函数真正返回。

checkQueryStringExists(request) {
    return new Promise(async (resolve, reject) => {
        var url_parts = url.parse(request.url, true);
        var api_token = url_parts.query.api_token;
        if(typeof api_token === 'undefined') {
            resolve(false);
        }
        resolve(true);
    }
});

router.post("/get-profile", async function(request, response) {
    response.setHeader('Content-Type', 'application/json');
    try{
        var result = await checkQueryStringExists(request);
        if(!result) {
            response.status(401).send(JSON.stringify({ "Status": false, "Message": "Invalid query string." }));
        } catch (e){
          console.log(e)
    }
});

答案 2 :(得分:0)

Node.js is asynchronous programming , it is not like c++ , or other language in which the code run one by one , you have to write callback or promise or async/await to run it in synchronous .

The thing i use is callback , you can use same way promise ansd asyn/await. :) Let me know you want any explanation on this.

  function checkQueryStringExists(request, callback) {
        var url_parts = url.parse(request.url, true);
        var api_token = url_parts.query.api_token;
        if(typeof api_token === 'undefined') {
            return callback(false);
        }
        return callback(true);
    }

    router.post("/get-profile", function(request, response) {
        response.setHeader('Content-Type', 'application/json');

        checkQueryStringExists(request,function(status){
                 if(!status) {
                      response.status(401).send(JSON.stringify({ "Status": false, "Message":"Invalid query string." }));
                    }else{
                      response.status(200).send(JSON.stringify({ "Status": true, "Message":"query string is ok." }));
                    }

          });

    });