Node.js从请求中提取POST数据(无express.js)

时间:2019-03-14 09:07:52

标签: node.js socket.io

我有一个服务器应用程序,该应用程序从一台服务器接收发布请求,然后将该数据中继到通过套接字(socket.io)连接的客户端。我在提取帖子正文时遇到了麻烦。该应用程序似乎没有进入.on('data', ...函数。有什么想法我要去哪里吗? 参考:https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/

var http = require("http");
var https = require('https');
var url = require("url");
var fs = require('fs');
var io = require('socket.io');
var util = require('util');
var srvHelper = require("./srvhelper");
var localstore = require("./localstore");
var prefs = require("./prefs");
var serverPrefs = prefs.readPrefs('server');
var socketPrefs = prefs.readPrefs('socket');
var ip = serverPrefs.ip;
var port = serverPrefs.port;
var sslKeyPath = serverPrefs.sslKeyPath;
var sslCertPath = serverPrefs.sslCertPath;
var socketAuthToken = socketPrefs.authToken;
var server;

var app = function(req, res) {

    // parse URL and Path
    var parsedUrl = url.parse(req.url, true);
    var path = parsedUrl.pathname;
    var fullPath = parsedUrl.path;

    // HTTP Basic Auth
    if (srvHelper.doBasicAuth(req, res)) {
        if (path == '/notifyuser' && req.method === 'POST') {
            prefs.doLog('now were in the post section');

            let body = [];

            req.on('error', function(err){
                prefs.doLog(err);
            }).on('data', function(chunk){
                body.push(chunk);
            }).on('end', function(){
                body = Buffer.concat(body).toString();
                console.log(body); // this is always null ??
                res.writeHead(200, {
                    'Content-Type': 'text/plain'
                });
                res.end('Success');
            });
        } else {
            srvHelper.throwHttpError(404, 'Not Found', res);
        }
    }
};

if ((sslKeyPath) && (sslCertPath)) {
    var sslOptions = {
        key: fs.readFileSync(sslKeyPath),
        cert: fs.readFileSync(sslCertPath)
    };
    server = https.createServer(sslOptions, app);
} else {
    server = http.createServer(app);
}

server.listen(port, ip);

仅供参考,POST请求来自一个oracle服务器,当表中的行被更新时会触发

apex_web_service.g_request_headers(1).name := 'content-type';
apex_web_service.g_request_headers(1).Value := 'application/json';

l_clob := APEX_WEB_SERVICE.MAKE_REST_REQUEST
                                ( p_url    => cqn_relay_url
                                , p_http_method => 'POST'
                                , p_body   => v_json_rows
                                , p_wallet_path => v_path_to_wallet
                                );

0 个答案:

没有答案