views:

584

answers:

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?

+1  A: 

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);
AHM
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.
Kuroki Kaze
Fire Crow
@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
A: 

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.

Jökull