Develop a blog system using express and js (1)

nodejs introduction and installation

Insert image description here

nodejs introduction

node.js is a javascript runtime environment based on the Chrome v8 engine. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient. The package manager npm provided by Node.js has become the largest open source ecosystem in the world.

To put it simply: used to develop server-side programs in js

Features: Single-threaded, asynchronous, event-driven.

After learning nodejs, we can do the backend! In addition, js can be used as the front end, and finally we can make a complete website.

nodejs official website

What nodejs can do

Node.js can parse js code (without browser security level restrictions) and provides many system-level APIs, such as: file reading and writing, process management, and network communication. . . .

To put it simply, it allows the execution of js to be separated from the browser.

Download and install

Download address https://nodejs.org/zh-cn/download/ LTS is a long-term stable version, just download this. To install, double-click nodejs.exe and take the next crazy step!

After the installation is complete, restart vscode or the system, then enter the terminal and execute the node -v command to check the nodejs installation version number. If the version number is output, the installation is successful, otherwise the installation fails. (If the installation fails, it is recommended to uninstall, then restart the computer and reinstall)

Verify using terminal

  1. Open the terminal, there are two methods, choose one of them
    1. Select any file in vscode, right-click and open it in the terminal. Note: It is best to restart vscode after installing nodejs!
    2. Open the folder where any file is located and enter cmd in the address bar of the folder
  2. In terminal node -v

Execute js files in node environment

Write an index.js file in your project and write any js code in it!

Create index01.js file

Enter the directory where index01.js is located in the terminal and execute it in the terminal

nodejs modular development

The so-called module is a file, which encapsulates an object or function of a certain function.

define module

ajax.js file

Load module

nodejs third-party module

For third-party modules, we need to use the npm tool that comes with node to download.

npm is similar to a software store. Any function written by programmers using js can be uploaded to the npm website, www.npmjs.co m

Of course we can use downloading. When downloading, we need to use the npm command. Next, we will learn how to use npm.

Use of npm

1-Go into the project folder

Right-click the folder in vscode and open it in the terminal

console.log("hello nodejs");

Execute in terminal:

node index01.js
var obj={
    
    
    get:function(){
    
    
    },
    post:function(){
    
    
    }
}
module.exports=obj
var obj=require('./ajax')
obj.get()
obj.post()

2-Initialize npm init

Directly enter npm init in the terminal to automatically create the package.json file. This file is used to record our installed third-party modules.

3- Install third-party modules locally

The third library module we need to use in our project is also called project dependency, which can also be said to be the module that the project depends on.

Order:

For example, our project will use the express module

Use the command npm i express to install the lodash module, and write the installation information to the package.json file. Install it locally. Express is downloaded to node_modules.

After local installation, there will be a record in the package.json file

4- Third-party modules installed in the project

5-Local installation development dependencies

nodemon-------Monitor file changes and re-execute the node command (similar to the lifeserver used in the front end)

If you want to use nodemon, you can use it with the npm script, or you can directly install nodemon globally and use it.

5-1 Use with local installation and npm script

nodemon is a monitor for changes in js files. If the js file changes, it will re-execute the js file.

npm i nodemon -D 开发依赖 (工具) npm i nodemon --save-dev

Cooperate with npm script

npm i 模块名

npm install express

"dependencies": {
    
    
"express": "xxxx"
}

let express = require('express')

"devDependencies": {
    
    
"nodemon": "^2.0.15"
}

Enter npm run aaa or npm start in the terminal

5-2 Directly install and use globally

npm i nodemon -g

After global installation, you can directly enter nodemon index.js in the terminal.

6-Other commands npm i

Automatically check package.json dependencies and download them automatically.

7-npm download source

Using npm directly to download may be slower

npmmirror.com is a complete npmjs.org mirror. You can use this instead of the official version (read-only). The synchronization frequency is currently 10 minutes Once to ensure that it is as synchronized with official services as possible

To change the download address of npm, just choose one of the following two options.

1-Modify the download address of npm

View the current npm download source

In the future, we will use npm to install the module and download it directly from npmmirror.

2- cnpm

If we adopt the second method, we can use the cnpm command in the future.

cnpm i express

Use of express framework

express introduction

"scripts": {
    
    
  "test": "echo \"Error: no test specified\" && exit 1",
  "aaa": "nodemon index.js",
  "start": "nodemon index.js"
}

[npm config set registry https://registry.npmmirror.com/](npm config set registry https://registry.npmmirror.com/)

[npm config get registry](npm config get registry)

[npm install -g cnpm --registry=https://registry.npmmirror.com](npm install -g cnpm --registry=https://registry.npmmirror.com)

Introduction to Express Express is a simple and flexible node.js web application framework that provides a series of powerful features to help you create various web applications, and rich HTTP tools. Use Express to quickly build a fully functional website.

Express framework core feature: middleware can be set up to respond to HTTP requests.

Routing tables are defined for performing different HTTP request actions.

HTML pages can be rendered dynamically by passing parameters to the template.

Express does not perform secondary abstraction on the characteristics of node.js itself, but expands on the basic functions.

express is a framework composed entirely of routing and middleware

Essentially, an express application is to call various middleware

Simply put, Express allows us to quickly create a web application using MVC (the front and back ends can be separated or not)

https://expressjs.com/ English official website

http://www.expressjs.com.cn/ Chinese official website

Guess you like

Origin blog.csdn.net/2301_78784268/article/details/131983386
Recommended