简体   繁体   English

从Node.js到Django的HTTP请求

[英]http request from nodejs to django

We have a django app and I need to add some functionality with NodeJS. 我们有一个django应用,我需要使用NodeJS添加一些功能。 I've been following this example: http://www.maxburstein.com/blog/realtime-django-using-nodejs-and-socketio/ using socket.io and it works perfect on localhost. 我一直在关注以下示例: http : //www.maxburstein.com/blog/realtime-django-using-nodejs-and-socketio/使用socket.io,它在localhost上运行良好。 Now, the problem comes when moving the application to apache server. 现在,将应用程序移动到apache服务器时出现了问题。 I need to send a POST request to django and all I get is a timeout response. 我需要向django发送POST请求,而我得到的只是超时响应。

Our django app over apache is listening on port 80 and node is listening on port 4000. The config for the http request is as follows: 通过apache的django应用正在侦听端口80,而节点正在侦听端口4000。http请求的配置如下:

var options = {
        hostname: 'hostaddress',
        port: '80',
        path: '/dashboards/datosinforme/',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': values.length
        }
    };
    var req = http.request(options, function(res){
        res.setEncoding('utf8');
        var data_obj = "";
        res.on('data',function(data){
            data_obj+=data;
        });
        res.on('end', function(){
            ...some functionality...
        });
    });
    req.write(values);
    req.end();

I've tried both local and public IPs. 我已经尝试过本地和公共IP。 I get no response from django using the public IP and Bad Request using the local IP. 使用公共IP的django没有响应,使用本地IP的Bad Request却没有响应。

I'd be eternally grateful if anyone could help. 如果有人可以帮助,我将永远感激不已。 Thanks in advance. 提前致谢。

Edit: Ok, I now tried with 127.0.0.1 as hostname (because both node and django are on the same machine) and I get a Bad Request response, but I don't really see anything wrong in it: 编辑:好的,我现在尝试使用127.0.0.1作为主机名(因为node和django都在同一台机器上),我得到一个Bad Request响应,但是我没有真正看到任何错误:

POST /dashboards/datosinforme HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 174
Host: 127.0.0.1:80
Connection: keep-alive

id=5&tipo_de_fecha=d&time_zone=Europe%2FBerlin

Ok, I got it. 好,我知道了。 I just had to include 127.0.0.1 in the ALLOWED_HOSTS parametter in settings.py and no more bad requests from django. 我只需要在settings.py的ALLOWED_HOSTS参数中包含127.0.0.1,而django不会再发出任何错误请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM