API node.js router.get async

时间:2015-08-28 14:57:52

标签: javascript node.js api asynchronous routes

我有这个代码/路线:

router.get('/:id?', function (request, response) {
    myfunction (fields, limit, function (err, users) {
        if (err != 'NULL') {
            response.statusCode = 500;
            response.send({
                status: 'error',
                error: err.code
            });
        } else {
            response.statusCode = 200;
            response.send({
                status: 'ok',
                date: new Date(),
                length: users['length'],
                json: users['rows']
            });
        }
    });
});

function getAll(fields, limit, callback) {
    var start = new Date().getTime();
    var myQuery = "SELECT " + fields + " FROM user_info";
    if (limit != "NULL") {
        var offset = String(new Date().getTime()).slice(-4);
        myQuery += " LIMIT " + offset + ", " +limit;
}

mysqlHandler.runQuery(myQuery, function (err, users) {
    if (err != 'NULL') {
        callback(err);
    } else {
        callback('NULL', users);
    }
});

function runQuery(myQuery, callback) {
index.pool.getConnection(function (err, connection) {
    try {
        if (err) {
            connection.release();
            callback({
                statusCode: 503,
                status: 'error',
                error: err.code,
                message: "Mysql error"
            });
        } else {
            connection.query(myQuery, function (err, rows, fields) {
                if (err) {
                    connection.release();
                    callback({
                        "err": err.code
                    });
                } else {
                    connection.release();
                    callback('NULL', {
                        "length": rows.length,
                        "rows": rows
                    });
                }
            });
        }
    } catch (error) {
        callback({
            statusCode: 503,
            status: 'error',
            error: err.code,
            message: "Connection error"
        });
    }
});
}

如果2个客户参加此路线,他们将无法同时获得答案,我的路线不是异步,为什么?

在其中,所有功能都是,但是在这个文件中,我的所有路由都没有,2个不同的路由将相互等待(第二个路由将等待第一个路径的结束)。

0 个答案:

没有答案