Front-end learning and simple use of node.js transcripts and important knowledge points

1 PHP writing back-end code requires Apache's HTTP server and some modules, but node js is different, it is "not only implements an application, but also implements the entire HTTP server."
2
Node.js consists of three parts: (1) Introduce the require module: use the require command to load the Node.js module
(2) Create a server: the server can listen to client requests, similar to Apache's http server
(3) receive requests And respond to the request: the client can use a browser or terminal to send an HTTP request, and the server returns response data after receiving the request.
3 Analyze the Node.js HTTP server: the
first line of request (require) the http module that comes with Node.js, and It is assigned to http variables. Next we call the function provided by the http module: createServer. This function returns an object. This object has a method called listen. This method has a numeric parameter that specifies the port number the HTTP server listens on.
4 Create server code:
var http = require('http');
http.createServer(function (request, response) { // Send HTTP header // HTTP status value: 200: OK // Content type: text/plain response .writeHead(200, {'Content-Type':'text/plain'}); // Send response data "Hello World"





response.end('Hello World\n');)).listen(8888);
// The terminal prints the following information
console.log('Server running at http://127.0.0.1:8888/');
5 following examples , We use the npm command to install the commonly used Node.js web framework module express:
npm install express After
installation, the express package is placed in the node_modules directory under the project directory, so in the code, you only need to pass require('express') The way is just fine, no need to specify the third-party package path.
var express = require('express')

6 The difference between global installation and local installation of modules
1 npm install express # local installation
npm install express -g # global installation

2 The saved path is different: the local is installed to the node-moudel specified in the project. The global installation is to node-mouduels under the node installation directory.

3. The usage is different: you can use the command line to operate the global installation. Locally, the installation module is acquired and installed through require syntax.

4. Can the global system replace the local system? No. After the global installation, if you want to reference the module locally, you need to modify the path.

5. The importance of local installation: The most important thing about local installation is to solve the problem of the version dependency of different projects on different packages.

7View installation information
You can use the following command to view all globally installed modules:
npm list -g 8Uninstall
modules
We can use the following command to uninstall Node.js modules.
$ npm uninstall express After
uninstalling, you can go to the /node_modules/ directory to check whether the package still exists, or use the following command to check:
$ npm ls
update module
We can use the following command to update the module:
$ npm update express

Search module
Use the following to search for modules:
$ npm search express
9 node.js processing requests: Node.js uses an event-driven model. When a web server receives a request, it shuts it down and processes it, and then serves the next web request.

Insert picture description here

Guess you like

Origin blog.csdn.net/MJ1314MJ/article/details/109011154