Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

node.js - 如何在Node.js中处理POST数据?(How to process POST data in Node.js?)

How do you extract form data ( form[method="post"] ) and file uploads sent from the HTTP POST method in Node.js ?

(如何提取Node.js中 HTTP POST方法发送的表单数据( form[method="post"] )和文件上传?)

I've read the documentation, googled and found nothing.

(我已经阅读了文档,谷歌搜索并没有发现任何东西。)

function (request, response) {
    //request.post????
}

Is there a library or a hack?

(有图书馆还是黑客?)

  ask by Ming-Tang translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the querystring module:

(您可以使用querystring模块:)

var qs = require('querystring');

function (request, response) {
    if (request.method == 'POST') {
        var body = '';

        request.on('data', function (data) {
            body += data;

            // Too much POST data, kill the connection!
            // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
            if (body.length > 1e6)
                request.connection.destroy();
        });

        request.on('end', function () {
            var post = qs.parse(body);
            // use post['blah'], etc.
        });
    }
}

Now, for example, if you have an input field with name age , you could access it using the variable post :

(现在,例如,如果您有一个名为ageinput字段,则可以使用变量post访问:)

console.log(post.age);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...