nodejs+express获取数据流
发布于 9 年前 作者 Ireoo 12269 次浏览 最后一次编辑是 8 年前 来自 问答

像php的php://input

可以使用 file_get_contents(“php://input”);

nodejs 里怎么表现呢,上次看到过,没记下来。。。。

6 回复

req本身是一个可读流… 可以用stream的方法:

//测试:curl -d "testdata" http://localhost:3003
var http = require('http');
var server = http.createServer(function(req, res) {
  req.on('data', function(chunk) {
    res.write(chunk);
  });
  req.on('end', function() {
    res.end();
  });
});

server.listen(3003);

然后很多中间件封装了处理各种content-type的流:

body-parser 能处理 application/json和application/x-www-form-urlencoded的数据,解析完放到req.body里。 multer 处理 multipart/form-data 的数据,解析或者存成存成文件,把处理信息放在req.file里。 raw-body 能把流数据整合到一个string里(在ios上传文件的时候可以用,content-type是application/octet-stream,需要自己判断,不影响其他中间件)。

哥们 express里使用stream用起来了吗

使用stream 和express 么关系把 普通web怎么获取,你就怎么获取就行了哦

@albin3 突然发现这个方式有字符数限制。。。。。

@hfqf 我要好好研究下了,谢谢

回到顶部