Basic knowledge of node.js and how to use npm

1. A brief introduction to Node.js

Node.js is JavaScript running on the server

In the node environment, through modular js language and functional programming, and without considering server compatibility issues, directly using the latest ES6 standard, it can fully meet the engineering needs.

1. Application scenarios:
  • Real-time applications: such as online multi-person collaboration tools, web chat applications
  • I/O-based high-concurrency applications: For example, the client provides an API to read the database
  • Streaming application: If the client frequently uploads files
  • Front and rear separation
2. The composition of the Node.js application
  • 引入 required 模块: Use the require command to load the Node.js module.

  • 创建服务器: The server can listen to client requests, similar to HTTP servers such as Apache and Nginx.

  • 接收请求与响应请求: The client can use a browser or terminal to send an HTTP request, and the server returns the response data after receiving the request.

Create a node.js application:

  1. Introducing required modules
    using requireinstructions to load http module, and examples of the assignment to variables HTTP http
var http = require('http');
  1. Create server

Using the http.createServer()method to create the server, and use the listen()method bind event listener port 8888. Function by request, responsereceiving response data and parameters.

http.createServer(function (request, response) {
    
    

    // 发送 HTTP 头部 
    // HTTP 状态值: 200 : OK
    // 内容类型: text/plain
    response.writeHead(200, {
    
    'Content-Type': 'text/plain'});
    
    // 发送响应数据 "Hello World"
    response.end('Hello World\n');
}).listen(8888);

// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');
  1. Use the node command to execute the above code:
//运行server.js文件
node server.js
//以下为运行结果
Server running at http://127.0.0.1:8888/

Insert picture description here
4. Open the browser and visit http://127.0.0.1:8888/, you will see a web page that says "Hello World".

2. How to use npm

NPM is a package management tool installed with NodeJS. It can solve many problems in NodeJS code deployment. Common usage scenarios are as follows:

  • Allow users to download third-party packages written by others from the NPM server to local use.
  • Allow users to download and install command line programs written by others from the NPM server for local use.
  • Allow users to upload their own packages or command line programs to the NPM server for others to use.
  1. Check if npm is installed
$ npm -v
  1. npm upgrade
npm install npm -g
  1. Install modules using npm
$ npm install <Module Name>

Example: $ npm install express (install express module)

After installation, the express package is placed in the node_modules directory under the project directory, and then use require('express') to import it

var express = require('express');
  1. Local installation and global installation
npm install express      (本地安装)
npm install express -g   (全局安装)
  1. Update module (the express module is taken as an example below)
$ npm update express
  1. Search module
$ npm search express
  1. Uninstall the module
$ npm uninstall express

3. REPL (Interactive Interpreter)

read-eval-print-loop --> Read-evaluate-output-loop, you can enter commands in the terminal and receive the response from the system.

  1. Such as performing simple expression operations...
> 1+4
5
> 5/2
2.5
> x=10
10
> var y=5
undefined
> x+y
15

The special symbol ' _' can indicate the result of the previous run:

> x=10
10
> y=5
5
> x+y
15
> var sum=3+_
undefined
> console.log(sum)
18
undefined
  1. Common REPL commands
instruction Explanation
ctrl + c Exit the current terminal.
ctrl + c press twice Exit the Node REPL.
ctrl + d Exit the Node REPL.
Up/down keys View historical commands entered
tab 键 List current commands
.help List the commands used
.break Exit multi-line expression
.clear Exit multi-line expression
.save filename Save the current Node REPL session to the specified file
.load filename Load the file content of the current Node REPL session.

Guess you like

Origin blog.csdn.net/isfor_you/article/details/113934679