Node.js

Node.js is a JavaScript runtime environment based on Chrome V8 engine. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient.

Installation Node. Js

There are many ways to install Node.js. To Download node.js for each platform, click on The Download website. On Mac/Windows we can download the installation package and install it just like any other software. Ubuntu can be installed with apt-get (CentOS can be installed with yum). Linux users recommend using source code.

You can find two versions of Node.js on the official website, LTS is the long term support version and Current is the latest version

After the installation is complete, run the following command on the CLI:

> node - v v10.7.0Copy the code

NRM Quickly switches over NPM sources

Due to China’s network environment, NPM source access is slow, so we can use NRM to change the source to domestic one

The installation

Or use NRM

npm install -g nrm
Copy the code

use

nrm ls * npm ----- https://registry.npmjs.org/ cnpm ---- http://r.cnpmjs.org/ taobao -- https://registry.npm.taobao.org/  nj ------ https://registry.nodejitsu.com/ rednpm -- http://registry.mirror.cqupt.edu.cn skimdb -- https://skimdb.npmjs.com/registry # with a source NRM use taobao taobaoCopy the code

Hello World

At this point, the Node.js environment is set up!

Now let’s actually do it. Here is an example from the official website, a Web server written in Node.js that returns ‘Hello World’.

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) = > {
  res.statusCode = 200;
  res.setHeader('Content-Type'.'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () = > {
  console.log(The server runs in http://${hostname}:${port}/ `);
});
Copy the code

Node.js related articles

  • Node.js tutorial
    • Build node.js environment
  • Actual project: Node.js + Koa2 + MongoDB + Aliyun Linux server, build blog and deploy online