nodejs simply set up a server

  Preface:

  nodejs main language background

  Custom Modules

  JavaScript is a language Reception

  nodejs are also using our JavaScript language, even his engines are the chrome v8 engine, open source, so learning nodejs front-end for us, it is a very friendly thing

  Background languages:

    php

    python

    Java

  nodejs advantage

    1. Performance  

      After a professional who does not test: nodejs 86 times higher than the performance of php 

                   nodejs run 1s php to run one and a half

              It is to write a project: php 200 servers a server 20000, a total of 4,000,000

                      nodejs 3 servers it is 60,000;

      Of course, this is not that bad php, after all, can become a well-known language background, how could so weak; moreover, nodejs short time, really old compared to the background of the language, there are still weaknesses

    2. The front desk js fit more convenient

    Currently: nodejs still play the role of a tool class time nodejs appear or short;

  nodejs really at the front, setting off a frenzy, we also have a lot of choice;

 

  instruction

    Nodejs program running in the popup window + r cmd input 

    Switching letter d: (d to disk)

    Cd into the directory directory name

    Run the program node file name

    Completion directory tab

    Terminate the program ctrl + c  

 

  js in usable nodejs can use

    Case: Date

      var oDate=new Date()

      console.log(oDate.getFullYear)

    Case: Regular

      var re = / d / g

      console.log("stds".match(re));

  We run through nodejs found something we print something printed on browsers

    

  By nodejs set up a server:

  nodejs write a server, others can gain access to;

  http protocol: browsers and servers comply with the specification;

  Http communication between server and browser, are achieved through http

  Our server, the most important part, listen, that is to say, http sends a request to the server, then the server should receive such a request, this is the monitor;

  The first step in creating our servers; (content written in our js file to)

  var http = require("http");

  var server = http.createServer (function (request, response) {// http.createServer () to create a server, the callback function parameter

    // two parameters request callback request in the inside, response response (we all know that the variable parameter, you can write with the change)

    console.log (request.url) // URL acquired the following parameter;

    response.write ( "123"); // data in response to a browser Response.Write ()

    response.end (); // finished each response, we need to call this method to end response

  })

  server.listen (8080) // parameter is the port listening

  

  This is the initial creation process

  

    

  

Guess you like

Origin www.cnblogs.com/shangjun6/p/11122255.html