How to Use stream in node. js: node. jsstream

Source: Internet
Author: User
Tags form post

How to Use stream in node. js: node. jsstream

I ran to learn OC in these days, but I haven't learned it yet. There is still a long time to leave the job. By the way, let's review the knowledge of node.

There are a lot of people coming and going in every language, but I can't do without node. I am not using it for development, but I am using a relatively large number of js, so it is better to study node, and stream has a high position in node. Let's take a look at this content in the spare time. On the way to node, I am a newbie.

Today, I downloaded the nodeschool course. Here is an example. (I modified a little bit)

var concat = require('concat-stream');var http = require('http');var qs = require('querystring');  var server = http.createServer(function (req, res) {    if (req.method === 'POST') {      req.pipe(concat(function (body) {        body = qs.parse(body.toString())        res.end(Object.keys(body).join('\n'));       }));    }    else res.end();  });  server.listen(5000);

This question means that if you reverse the post data, I will not do it. The principle is the same, but the requirements are different. We use the concat plug-in to direct the data stream to concat when the form is submitted. In this example, we use the data from form post.

For testing, I use the request library.

Var request = require ('request') request. post ('HTTP: // 127.0.0.1: 808080', {form: {"name": "ryan", "age": 23 }}, function (err, res, body) {console. log ('Received successfully: ') console. log (res ['body']) // name age })

Start the server and run the test. We can see that this object has been post. Concat-stream is mainly used for buffer connection. I understand that when you transmit data in the form of buffer, You can transmit data of any type. In npmjs, it transmits an image. Since we get the data of this image after receiving it, we can use this to upload, copy, and share the same principle.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.