node. js Server and Data flow

Source: Internet
Author: User

1.Node is often used to build servers, and the following code creates a server.

var http = require (' http '); var server = http.createserver (); Server.on (' require ',function(req,res) {    Res.writehead (200,{' content-type ': ' Text/plain ');    Res.end (' hello,world\n '); }) Server.listen; Console.log (' server running at Http://localhost:3000/');

Use the Createserver () method primarily.

2.Node is also powerful in data flow and data flow. by transferring data one piece at a A, the developer can start processing each piece of data without waiting for all the data to be processed. Here's an example of how to work with JSON data in a data flow:

var stream = Fs.createreadstream ('./resource.json ') stream.on (' data ',function(chunk {    Console.log (chunk)}) Stream.on ("End",function() {    console.log ("finished")  )})

3. Borrow the previous HTTP server to see how a picture flows to the client:

var http = require ("http"); var fs = require ("FS"); http.createserver(function(req,res) {    Res.writehead ( 200,{"Content-type": "Image/png"});    Fs.createreadstream ("./image.png"). Pipe (res);}). Listen (console.log); ("Server running at Http://localhost:3000/");

node. js Server and Data flow

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.