technology

607Currently online
200Today's Reading
15Share Today
Multilingual display

Node.js develops Web backend services

2018-03-25 08:00:59

Node.js is a JavaScript runtime environment based on Google Chrome V8 engine. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient. npm, the Node.js package manager, is the world's largest open source library ecosystem. Easy to build fast and scalable network applications, Node.js uses an event-driven, non-blocking I/O model to be lightweight and efficient, making it ideal for data-intensive real-time applications running on distributed devices. Official website: nodejs.org/en/ Chinese: cnodejs.org, nodejs.cnAPI: Node.js is simply running on the server side of JavaScript, is now popular language can run in the front end and the background of the program language, you can think of JavaScript like Java and C#. Related technologies: Database: MongoDB, non-relational database, NoSQL (Not only SQL) MVC framework: AngularJSWeb server: Express template engine: jade, ejs, htmljs, swig, hogan.js

Tools/Materials
1

Express

2

nodejs

Step 1: Set up the Node.js development environment
1

Download the latest version of Node.js from the official website and install it step by step as prompted. If the installation fails, install it manually and set the installation location of Node.js to the path of the environment variable.

2

After the installation is complete, start the command line and test

Step 2: Develop the Node.js plugin using IDEA
1

Download and install IntelliJ IDEA

2

The default installation of NodeJS in IDEA is complete. In file -> setting ->Plugins of IDEA, the default on the right side is that there is no such component. You need to manually click Browe repositories. Search for nodejs in the list of plugins, you will see the NodeJS plugin, click Download, restart, (in fact, it will be associated with your installed NodeJS). There is one more NodeJS in the new project

3

Build the first NodeJS project as the default operation, the resulting project directory structure is as follows, the change directory structure is the same as the Node.js development Guide example, and IDEA has generated a working Demo. Click the green arrow in the upper right corner to start the service. If the message "Listening on port 3000" is displayed, the service is successfully started. Enter "" in the browser, and the following figure shows that the service is successfully started

Step 3: Write the first Node.js program
1

In the above example, we are compiled and run through the IDE, in fact, you can also run manually, write a piece of code as follows: server.js

2

// Rely on an http module, equivalent to import in java, and usingvar http = require('http') in C#; // Create a server object server = http.createServer(function (req, res) {// Set the MIME of the response header to plain text res.writeheader (200, {"Content-Type": "text/plain"}); // Outputs the character res.end("Hello World\n") to the client; }); // Enable the server to listen to local port 8000 and start server.listen(8000,'127.0.0.1'); console.log("server is runing at 127.0.0.1:8000"); Copy the above code into idea's server.js file

3

Explaining the run in a node environment: Note: Always remember to write node before

4

Result: The required module is introduced: we can load the Node.js module using the require directive. Create a server: The server can listen to client requests, similar to HTTP servers such as TomCat, IIS, Apache, Nginx, etc. The request server is easy to create, the client can use a browser or terminal to send HTTP requests, and the server receives the request and returns the response data. The first line requires the http module that comes with Node.js and assigns it to an http variable. Next we call the function provided by the http module: createServer. This function returns an object with a method called listen, which takes a numeric argument specifying the port number on which the HTTP server listens.

Matters needing attention

If there are other problems, you can learn from:

Recommendation