node. JS Learning Notes under Windows (5)---Creating Servers and clients with node. js

Source: Internet
Author: User

// introducing the HTTP module var http = require (' http '); // Call HTTP Createserver method, this method has a callback function, this callback number // the function is to execute this callback function http.createserver (function  (req, res) {  //) until the request is sent to the server.  Send  res.end (' Hello world\n ');}). Listen (3000, "127.0.0.1"); // port and IP bindings console.log (' Server running at Http://127.0.0.1:3000/');

The above code, create a server, and set the content sent to the client, the following talk about the redirection in node. js

var http = require (' http '); Http.createserver (function  (req, res) {  // redirect , Writehead method  res.writehead (301, {    ' location ': ' Http://www.baidu.com '   });    Res.end ();}). Listen ("127.0.0.1"); Console.log (' Server running at Http://127.0.0.1:3000/');

By setting up a route to respond to different requests ( essentially ), this is actually getting more complicated, because if there are many kinds of responses, If--else will be more and more, and the express framework will be introduced later.

varHTTP = require (' http '), URL= require (' URL '); Http.createserver (function(req, res) {//parse URL, get path name  varpathname =Url.parse (req.url). Pathname; if(Pathname = = = '/')) {Res.writehead (200, {      ' Content-type ': ' Text/plain '    }); Res.end (' Home page\n ')  } Else if(Pathname = = = '/about ')) {Res.writehead (200, {      ' Content-type ': ' Text/plain '    }); Res.end (' About us\n ')  } Else if(Pathname = = = '/redirect ')) {Res.writehead (301, {      ' Location ': '/'    });  Res.end (); } Else{Res.writehead (404, {      ' Content-type ': ' Text/plain '    }); Res.end (' Page not found\n ')}). Listen (3000, "127.0.0.1"); Console.log (' Server running at Http://127.0.0.1:3000/');

Create a client with node. js

 var  http = require (' http ' , Port:  80 '/' };http.get (Options ,  function   (res) { if  (Res.statuscode = = 200 "The site is up!"  );  else   {Console.log ( "The site is down! "  );  }}). On ( ' error ', function   (E) { Console.log ( There is an error: "+ e.message);});  

node. JS Learning Notes under Windows (5)---Creating Servers and clients with node. js

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.