Node.js 作为 Django 的自定义(流式)上传处理程序

发布于 2024-08-24 20:50:48 字数 297 浏览 13 评论 0 原文

我想使用 Django 构建一个以上传为中心的应用程序。一种方法是使用 nginx 的上传模块(非阻塞),但它有其问题。 Node.js 应该是此类应用程序的良好候选者。但是我怎样才能让node.js充当 upload_handler() 用于 Django?我不确定在哪里寻找示例?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间不值得 2024-08-31 20:50:49

好吧,我不像这个主题或任何方面的专家,但我认为我理解它的方式,nginx 是一个在为您的 django 应用程序提供服务的网络服务器前面运行的代理,对吧?

您可以构建一个简单的 Node.js 服务器来执行相同的操作 - 侦听端口 80,等待请求完全发送到服务器,然后将其转发到为 Django 应用程序提供服务的 Web 服务器。如果您的问题是网络服务器线程被长时间运行的上传耗尽,那么我认为这可以解决该问题。

这是一些代码——就在我的脑海中

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);

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);
滴情不沾 2024-08-31 20:50:49

让 node.js 将文件写入磁盘,并将上传 POST 以及“_node_file”值代理到 Django,以便您的 Django 视图知道从哪里获取文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文