2

I want to build an upload-centric app using Django. One way to do this is with nginx's upload module (nonblocking) but it has its problems. Node.js is supposed to be a good candidate for this type of application. But how can I make node.js act as an upload_handler() for Django? I'm not sure where to look for examples?

2 Answers 2

2

Okay I'm not like an expert on the subject or anything, but the way I think I understand it, nginx is a proxy that runs in front of the webserver that serves your django app, right?

You could build a simple node.js server that does the same - listen on port 80, wait until the request is completely sent to the server, and then forward it to the webserver that serves the Django app. If your problem is that the webservers threads are being used up by long running uploads, then I think this would solve that problem.

Here's some code - just off the top of my head

var http = require('http');

var server = http.createServer(function (req, res) {
    var headers = req.headers;
    var url = req.url;
    var method = req.method;
    var body = '';
    req.addListener('data', function(chunk) {
        body += chunk;
    });
    req.addListener('end', function() {
        // at this point the request is completely uploaded (including files)
        // so it can be forwarded to the django webserver

        var dj_client = http.createClient(8000, 'localhost');
        var dj_req = dj_client.request(method, url, headers);
        dj_req.addListener('response', function (dj_res) {
            // here the response have been received from the django server
            // so we can return that to the waiting client
            res.writeHead(dj_res.status, dj_res.headers);

            dj_res.addListener('data', res.write);
            dj_res.addListener('end', res.close);
        });

        // send the request to the django webserver
        dj_req.write(body);
        dj_req.close();
    });
});

server.listen(80);
2
  • Seems like you're buffering the whole thing in memory. That's not streaming. I'm not that into Django, but it seems it will accept download by chunks. Apr 2, 2010 at 7:36
  • @Kuroki - true, but I read the original post as if that was the point. To avoid stalling the webservers thread for the duration of the upload, that is avoid streaming directly to the django app and instead buffer it with node.js.
    – AHM
    Apr 23, 2010 at 13:23
0

Make node.js write the file to disk and proxy the upload POST to Django along with a '_node_file' value so your Django view knows where to get the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.