A brief discussion on how to use Node third-party framework Express
This article will take you to learn about the third-party framework ExpressNode, and briefly talk about how to use the Express framework well. I hope it will be helpful to everyone!
1.Express framework introduction
- ##1.Express is developed by
- If you don’t even know how to use Express, you are basically embarrassed to tell others that you know NodeJS
##2 .Express official website: -
- www.expressjs.com.cn/
- expressjs.com/
-
General When we learn a new technology, we always go to the official website documentation to view its API, and then try a lot. Practice makes perfect
3. Express’s github address: https://github.com/expressjs/express -
The original author of Express, TJ, is very famous in the node community. He has written more than 200 frameworks. Currently, he has handed over Express to a friend for maintenance. , announced that it will no longer maintain the NodeJS framework and switch to the Go language (https://github.com/tj)
-
- A very important highlight of Express is that it does not change the existing features of nodejs, but expands on it
- In other words, using Express you can use any nodejs native API, or you can use Express’s API
- A very important highlight of Express is that it does not change the existing features of nodejs, but expands on it
- 5. The three core functions of Express
- #1. Hosting static resources
-
The nodejs implementation of static server discussed on the second day The function only requires one line of code in express
-
express has its own routing function, making Node server development extremely simple
- express supports chain syntax, which can make the code look more concise
-
The core technology and idea of Express, everything is middleware
-
Although middleware is a bit difficult to understand, it is very convenient to use, similar to
bootstrap plug-in- .
-
2. Download express -
The nodejs implementation of static server discussed on the second day The function only requires one line of code in express
Download instructions:
npm i expressIf your website is very slow, you can use npm config set registry registry.npm.taobao.org/ to increase the speed
就是淘宝帮你把这个东西下载淘宝的服务器上,然后你在淘宝服务器上下载
3.Use Express
//1.导入模块
const express = require('express')
//2.创建服务器
/* express() 相当于http模块的http.createServer() */
const app = express()
//3.接收客户端请求
/*(1)express最大的特点就是自带路由功能,我们无需在一个方法中处理所有请求
* 路由:一个请求路径对应一个方法(函数)
(2)在express中,每一个请求都是一个单独的方法
*/
app.get('/',(req,res)=>{
//响应客户端数据
//express响应数据 send方法:自动帮我们设置好了响应头,无需担心中文乱码问题
res.send('月下风起')
})
app.get('/heroInfo',(req,res)=>{
res.send({
name:'张三',
age:20
})
})
//4.开启服务器
app.listen(3000,()=>{
console.log('服务器启动成功')
})
4-Express to respond to client data
//1.导入模块
const express = require('express')
//2.创建服务器
/* express() 相当于http模块的http.createServer() */
const app = express()
//3.接收客户端请求
//文本类型数据
app.get('/',(req,res)=>{
//响应客户端数据
res.send('月下风起')
})
//json格式数据
app.get('/info',(req,res)=>{
//express自动帮我们将js对象转成json响应给客户端
res.send({
name:'张三',
age:20
})
})
//文件类型数据
app.get('/login',(req,res)=>{
res.sendFile(__dirname + '/login.html')
})
//4.开启服务器
app.listen(3000,()=>{
console.log('服务器启动成功')
})
5.Express Hosting static resources
http://expressjs.com/en/starter/static-files.html//1.导入模块
const express = require('express');
//2.创建服务器
const app = express()
//托管静态资源(相当于我们之前写的静态资源服务器)
/*
1.当请求路径为/时,express会自动读取www文件夹中的index.html文件响应返回
2.当路径请求为www文件夹中的静态资源,express会自动拼接文件路径并响应返回
*/
app.use(express.static('www'))
//4.开启服务器
app.listen(3000,()=>{
console.log('success')
})
6. Use of third-party middleware
1. On the Express official website, there are many third-party middleware, which can make our Nodejs development extremely simple
-
Middle It is a plug-in for the front-end of software. After use, it will add members to req or res in express
-
1. Go to the official website and check the documentation
- 2. CTRL C and CTRL V
-
One: Install
- npm i xxxx
- (copy and paste from the official website)
Third party Middleware needs to be installed using npm, which can be understood as a special third-party module app.use(xxx) - (Official website copy and paste)
- (copy and paste from the official website)
-
Install body-parser:
npm install body-parserhttps://www.npmjs.com/package/body-parser
//导入模块 const express = require('express') //创建服务器 const app = express() //使用第三方中间件 /*所有的第三方模块思路都是一样 1.进官网,查文档 2.找examples(使用示例),复制粘贴 a.安装第三方模块:`npm i body-parser` b.使用中间件: arr.use(具体用法请复制粘贴) 使用body-parser中间件之后,你的req会增加一个body属性,就是你的post请求参数 */ //(1)导入模块 const bodyParser = require('body-parser') // parse application/x-www-form-urlencoded //(2)使用中间件 app.use(bodyParser.urlencoded({ extended: false })) //解析json参数 app.use(bodyParser.json()) app.post('/abc',(req,res)=>{ console.log(req.body) //告诉客户端我收到的参数 res.send(req.body) }) app.post('/efg',(req,res)=>{ console.log(req.body) //告诉客户端我收到的参数 res.send(req.body) }) //开启服务器 app.listen(3000, () => { console.log('success'); })
The above is the detailed content of A brief discussion on how to use Node third-party framework Express. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools