Node.js+Express.js

Introduction to Node.js

Generally, we run Javascript in the browser. With Node.js we can run it on the computer.

Very convenient to set up a server.
View Node version

You can run node in the terminal
Exit

There is a special object in JavaScript called Global Object (Global Object), it and all its properties can be accessed anywhere in the program, namely global variables.
In browser JavaScript, usually window is the global object, while the global object in Node.js is global. All global variables (except global itself) are attributes of the global object.
Insert picture description here

Running Script.js

Insert picture description hereInsert picture description here

ES2020:globalThis

globalThis is a global variable, corresponding to window in the browser, and global in other places.

Modules

The lower version of Node does not have Import and Export. Use require('path') and Module.exports
Insert picture description here
Insert picture description here
ES6 module
needs a version than versison12.0.

Insert picture description here
But it will report an error SyntaxError: Cannot use import statement outside a module
because it is not run as an ES file.
Insert picture description here
Solution 1:
Change the extension from .js to .mjs
Solution 2:
Add "type" in Package.json :{"Module"}
Run Package.json
Insert picture description here
Insert picture description here

Type of Module

There are three types of modules in Node. The
first one is a module created by yourself, which can be imported with require or import.

The second is the module that
is installed in Node in advance by build-in-module . For example,'fs', files can be read.

The third type is package from npm
nodemon to monitor any changes in the node.js application and automatically restart the service, which is very suitable for use in a development environment.
nodemon will monitor the files in the startup directory, and if any file changes, nodemon will automatically restart the node application.
Insert picture description here
Insert picture description here
"Start": "nodemon" then npm start
Insert picture description here

Building a server

If you want to use require(), you need to add the "type":{"module"} in package.json before, otherwise an error ReferenceError: require is not defined will be reported

In the HTTP connection, the message is divided into two types: request and response.
The request is equivalent to the request sent from the front end.
response is the returned content, which will be displayed on the web page

HTTP request method
https://www.runoob.com/http/http-methods.html

Insert picture description here

Express.js

Express is a flexible Node.js web application development framework that keeps the smallest scale
Insert picture description here

Express middleware processes the request in the middle, and then performs the following steps after next().
Insert picture description here

RESTful API

The rest api is the best practice for separating the front and back ends. It is a set of standards or specifications for development, not a framework.
Get: receive the resource
Put: Change the state
Post:Create resource
Delete: remove the resource

Node File System Module

Insert picture description here

Guess you like

Origin blog.csdn.net/fragile98/article/details/112363562