如何解决状态:Node.js出现400错误

时间:2020-10-15 16:18:31

标签: node.js

我在以下Node.js代码中收到状态400错误 这正在尝试将https发布请求发送到端口12345上的abc.xyz 我无法弄清楚这里出了什么问题。 虽然我不是Node.js专家

const https = require('https');

const options = {
  hostname: 'abc.xyz',
  port: 12345,
  path: '/test/',
  method: 'POST',
  json: true,
  rejectUnauthorized: false
};


exports.handler = (event, context, callback) => {
    const req = https.request(options, (res) => {
        let body = '';
        console.log('Status:', res.statusCode);
        console.log('Headers:', JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', (chunk) => body += chunk);
        res.on('end', () => {
            console.log('Successfully processed HTTPS response');
            // If we know it's JSON, parse it
            if (res.headers['content-type'] === 'application/json') {
                body = JSON.parse(body);
                // console.log('Body: ' + body);
            }
            callback(null, body);
        });
    });
    req.on('error', callback);
    // req.write(JSON.stringify(event.data));
    //console.log(event.data);
    req.end();
};

1 个答案:

答案 0 :(得分:0)

在测试时,您只能在本地主机上托管这些应用程序。
您还需要一个app.listen(LOCALHOST:PORT);
一个简单的Node.js教程将为您提供更深入的说明。 这是一台运行在localhost 8080上的简单节点服务器,没有其他人可以看到它。

var http = require('http');

//create a server object:
http.createServer(function (req, res) {
  res.write('Hello World!'); //write a response to the client
  res.end(); //end the response
}).listen(8080); //the server object listens on port 8080