Почему запрос HTTP никогда не заканчивается - PullRequest
0 голосов
/ 23 апреля 2019

Я выполняю http-запрос в nodejs, но запрос никогда не заканчивается и продолжаю выполнять следующий код. он только печатает «конец ответа», но никогда не «конец запроса». Почему он будет вести себя так?

const post_data = JSON.stringify({
    'username': username,
    'org': orgName,
    'peers': peers,
    'channelName': channelName,
    'chaincodeName': chaincodeName,
    'fcn': fcn,
    'args': args
});

var post_options = {
    host: 'localhost',
    port: '3000',
    path: '/cc/write',
    method: 'POST',
    headers: {
         "content-type": "application/json",
         "Content-Length": Buffer.byteLength(post_data)
    }
};

var post_req = http.request(post_options, function (res) {
    console.log("connected");
    res.on('data', function (chunk) {
        console.log('Response: ' + chunk);
        ccResponse = chunk;
    });
    res.on('end', function () {
        console.log('response end')
    });
});

// post the data
post_req.write(post_data);
post_req.on('end', function () {
    console.log('request end');
});
post_req.on('finish', function () {
    console.log('request end');
});
post_req.end();

console.log("Random thing");

1 Ответ

0 голосов
/ 23 апреля 2019

Над последней строкой есть дополнительная скобка console.log("Random thing"); После снятия этой скобки код выдает этот журнал:

Random thing
request end
connected
Response: {"error":"Not Found"}
response end

Как видите, в журнале есть «конец запроса». Он был запущен с узлом v11.13.0.

Пожалуйста, дайте мне знать, если это не поможет.

Фактический код, который я использовал:

const http = require('http');

const post_data = JSON.stringify({
    'username': 'username',
    'org': 'orgName',
    'peers': 'peers',
    'channelName': 'channelName',
    'chaincodeName': 'chaincodeName',
    'fcn': 'fcn',
    'args': 'args'
});

var post_options = {
    host: 'localhost',
    port: '3000',
    path: '/cc/write',
    method: 'POST',
    headers: {
         "content-type": "application/json",
         "Content-Length": Buffer.byteLength(post_data)
    }
};

var post_req = http.request(post_options, function (res) {
    console.log("connected");
    res.on('data', function (chunk) {
        console.log('Response: ' + chunk);
        ccResponse = chunk;
    });
    res.on('end', function () {
        console.log('response end')
    });
});

// post the data
post_req.write(post_data);
post_req.on('end', function () {
    console.log('request end');
});
post_req.on('finish', function () {
    console.log('request end');
});
post_req.end();

console.log("Random thing");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...