Node.js as a custom (streaming) upload handler for Django

By : Gijs

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?

By : Gijs


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.

By : Jökull

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

By : AHM

This video can help you solving your question :)
By: admin