NodeJS服务器:POST请求后向客户端发送响应时出现问题

时间:2015-07-04 08:01:49

标签: javascript node.js api post oauth

我正在实现一个带有/ login端点的nodejs服务器,它通过端口8080接受POST请求。该脚本旨在通过发送一个来自不同服务器的访问令牌(在这个例子中,服务器是端口80上的localhost) POST请求包含用户名,密码和oAuth客户端凭据。然后该脚本用于将响应发送回客户端。

我遇到的问题是将响应发送回客户端。当我从POST请求中获取数据时,我认为我能够设置响应,但实际上并没有按预期工作。客户告诉我没有回应。

我以前玩过nodejs,但我还是个新手。那么请你告诉我在这里可以做得更好吗?

这是我的代码:

var sys = require("sys"),
    http = require("http"),
    path = require("path"),
    url = require("url"),
    qs = require('querystring');

var host = 'localhost';
var port = '80';

var client_id = '31479141e3793d9557489a0323d828ce_3dv4vdoffaecc4ogg8cwwcsg0wog4sc8wgo0g008sgcgkos4k0';
var client_secret = '143sn02rzarkwkwck0ocskgsgwocw4s0cscgwow8kck8ck40cs';
var grant_type = 'password';

http.createServer(function(request,response){
    var accessedPath = url.parse(request.url).pathname;

    if (accessedPath === '/login') {
        sys.puts('Accessed: ' + request.method + ': ' + accessedPath);

        if (request.method === 'POST') {
            var body = '';
            request.on('data', function (data) {
                body += data;
                if (body.length > 1e6)
                    request.connection.destroy();
            });

            request.on('end', function () {
                var post = qs.parse(body);

                if ('undefined' !== typeof post.username && 'undefined' !== typeof post.password) {
                    var loginData = qs.stringify({
                        'client_id': client_id,
                        'client_secret': client_secret,
                        'grant_type': 'password',
                        'username': post.username,
                        'password': post.password
                    });

                    var post_options = {
                        host: host,
                        port: port,
                        path: '/oauth/v2/token',
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded',
                            'Content-Length': Buffer.byteLength(loginData)
                        }
                    };

                    var post_req = http.request(post_options, function(res) {
                        res.setEncoding('utf8');
                        res.on('data', function (resData) {
                            console.log(resData); // resData is displayed properly in console
                            var responseData = qs.stringify(resData);

                            console.log(res.statusCode); // StatusCode is displayed property in console

                            // The following seems to not work, client receives no response
                            response.writeHead(res.statusCode, {'Content-Type':'application/json'});
                            response.write(responseData);
                            response.end();
                        });
                    });

                    post_req.write(loginData);
                    post_req.end();
                }
            });
        } else {
            response.writeHead(400, {'Content-Type':'text/plain'});
            response.write('Bad request\n');
            response.end();
        }
    }
}).listen(8080);
sys.puts("Server Running on 8080");

由于

0 个答案:

没有答案