?? node. js is a JavaScript runtime (runtime), released in May 2009, developed by Ryan Dahl, essentially encapsulating the chrome V8 engine. node. JS optimizes some of the special use cases, providing an alternative API that allows V8 to run better in a non-browser environment.
?? The V8 engine executes JavaScript very fast and performs very well. node. JS is a platform built on the chrome JavaScript runtime to easily build responsive, easy-to-scale Web applications. node. js is lightweight and efficient with event-driven, non-blocking I/O models, making it ideal for running data-intensive real-time applications on distributed devices.
?? node. js Download URL: http://Nodejs.org.
?? Node. js's introduction URL: https://www.sitepoint.com/node-js-is-the-new-black/.
?? After you install node. js, add node. js to the system path (default when installed), start CMD, enter node to enter node. js, and enter the command:
console.log(‘Hello world!‘);process.exit();node -v
Results such as:
where Process.exit () is exiting node. js, Node-v is the version that displays node. js.
?? Let's take a look at two simple examples.
?? The first example is simply to display text in a Web page.
?? Create a new Hello.js file with the following code:
varhttp= require(' http ');varServer= http.Createserver();Server. on(' request ', function(req,Res{ Res.Writehead( $, {' Content-type ':' Text/plain '}); Res.Write(' Hello World from node.js!\ n'); Res.Write(' A Simple example of node.js! '); Res.End();});varPort= 8080;Server.Listen(port);Server.once(' listening ', function(){ Console.Log(' Hello World Server listening on port%d ',Port;});
Placing the Hello.js file under the current path and entering the command ' node Hello.js ' in cmd, the following is displayed in cmd:
Enter http://127.0.0.1:8080 or http://localhost:8080 in the browser, and the page appears as follows:
?? The second example is to display text in a local text document in a Web page.
?? The new readfile.js is placed under the current path with the following code:
varhttp= require(' http ');varFs= require(' FS ');varServer= http.Createserver();Server. on(' request ', function(req,Res{ Res.Writehead( $, {' Content-type ':' Text/plain '}); varData= FS.Readfilesync("E:/input.txt"); Res.Write(' Read data from the local file system:\ n'); Res.Write(Data.toString()); Res.End();});varPort= 8080;Server.Listen(port);Server.once(' listening ', function(){ Console.Log(' Reading file E:/input.txt ');});
The contents of Input.txt in e-disk are as follows:
Enter node Readfile.js in cmd, as shown below:
Enter http://localhost:8080 in the browser and the page appears as follows:
This share to the end, welcome to Exchange ~ ~
node. js Introduction