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: Chinese:, 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





Step 1: Set up the Node.js development environment

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.


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

Step 2: Develop the Node.js plugin using IDEA

Download and install IntelliJ IDEA


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


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

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


// 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,''); console.log("server is runing at"); Copy the above code into idea's server.js file


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


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: