官网:实为npm第三方包,由于http效率太低,其提供了快速创建 Web 服务器的方法
简单使用
npm install express
// -导入 express
const express = require('express');
// -创建 web 服务器
const app = express();
// - 监听客户端的GET和POST请求,并向客户端响应具体的内容
app.get('/user', (req, res) => {
res.send({name: req.query.name , age: 20, gender: '男'}) // 向客户端响应一个JSON对象,req.query可将查询字符串“?name='name'”打包成一个对象
})
app.post('/user', (req, res) => {
res.send('请求成功')// 向客户端响应一个文本字符串
})
// -启动 web 服务器
app.listen(80, () => {
console.log('express server running at http://127.0.0.1');
})
app.use(express.static('./public'));# 静态资源 如http://localhost:3000/public/images/kitten.jpg可直接访问
// 挂载路径前缀
app.use('./file',express.static('./public'));
路由 app.METHOD(‘PATH’,HANDLEfunc);
//挂载路由(简单)
// 和之前的方法一样,直接挂载到app上
//处理对于/address的get和post请求:
app.post('/address',处理函数);
app.get('/address',(req,res)=>{})
//挂载路由(模块化)
// 创建路由模块
const express = require('express')
const app = express()
//1.导入路由模块
const router = require('./03.router')
//2.注册路由模块
app.use(router)
app.listen(80, () => {
console.log('http:127.0.0.1')
})
03.router.js:
中间件(中间处理环节)
next函数
// -导入 express
const express = require('express');
// -创建 web 服务器
const app = express();
// 常量mw所指向的,就是一个中间件函数
const mw = function (req, res, next) {
console.log('这是一个最简单的中间件函数')
next()
}
app.use(mw) // 将mw注册为全局生效的中间件
// **注意**:请求Home在当前中间件的业务处理完毕后,才调用next()函数,把流转关系转交给下一个中间件或路由,由于本文只有一个中间件,所以下一步执行“res.send('Home')”
app.get('/',(req, res)=>{res.send('Home')})
app.get('/user',(req, res)=>{res.send('uesr')})
// -启动 web 服务器
app.listen(80, () => {
console.log('express server running at http://127.0.0.1');
})
局部生效的
const express = require('express')
const app = express()
/*****************************************************************/
const mw1 = (req, res, next) => {
console.log('调用了局部生效的中间件')
next()
}
app.get('/', mw1, (req, res) => {
res.send('Home page.')
})
/*****************************************************************/
app.listen(80, () => {
console.log('http://127.0.0.1')
})
express
app.use(express.static(__dirname + '/public'))
.set('views', __dirname + '/views')
.engine('html', engine.mustache)
.set('view engine', 'html')
.get('/', (req, res) => res.render('index.html'))
.get('/index.html', (req, res) => res.render('index.html'));
-
app.use(express.static(__dirname + '/public'))
:express.static
是 Express 框架的一个中间件,用于提供静态文件服务。- 这行代码告诉 Express 在访问根路径时,查找并提供位于项目根目录下的
public
文件夹中的静态文件。这样,你可以通过浏览器直接访问这些文件,如样式表、脚本等。
-
.set('views', __dirname + '/views')
:- 使用
.set
方法设置 Express 应用的配置项。 - 这里将视图文件(模板文件)的根目录设置为
views
文件夹,该文件夹位于项目根目录下。
- 使用
-
.engine('html', engine.mustache)
:- 使用
.engine
方法设置视图引擎。这里使用了一个叫做mustache
的引擎来处理.html
文件。 - 视图引擎负责将模板渲染成最终的 HTML 输出。
- 使用
-
.set('view engine', 'html')
:- 使用
.set
方法设置视图引擎的默认扩展名。这里将默认视图引擎的扩展名设置为.html
。
- 使用
-
.get('/', (req, res) => res.render('index.html'))
:- 使用
.get
方法设置一个路由,处理 HTTP GET 请求。 - 当访问根路径
/
时,执行回调函数(req, res) => res.render('index.html')
。 - 回调函数中使用
res.render
渲染视图,即将index.html
渲染成最终的 HTML 响应。
- 使用
-
.get('/index.html', (req, res) => res.render('index.html'))
:- 同样使用
.get
方法设置一个路由,处理 HTTP GET 请求。 - 当访问路径
/index.html
时,执行回调函数(req, res) => res.render('index.html')
。 - 这个路由的效果和前一个路由一样,都是渲染
index.html
。
- 同样使用