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