Skip to content

Commit 26ae613

Browse files
author
142vip.cn
committed
feat(express): 新增模板项目文档、代码
1 parent c926cbd commit 26ae613

File tree

26 files changed

+948
-29
lines changed

26 files changed

+948
-29
lines changed

code/node/express/apps/quick-start/Readme.md renamed to code/express/apps/quick-start-demo/Readme.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
21
# Express框架快速开始演示Demo
32

43
## 项目初始化
54

65
```bash
6+
## 初始化
77
npm init
88
```
99

@@ -33,6 +33,12 @@ pwd
3333
node app.js
3434
```
3535

36+
或者使用`package.json`中的脚本,启动服务
37+
38+
```bash
39+
npm run dev
40+
```
41+
3642
服务运行,监听`3000`端口
3743

3844
## 调用服务
@@ -42,6 +48,11 @@ node app.js
4248
curl -i http://127.0.0.1:3000
4349
```
4450

45-
接口返回:Hello World
51+
接口返回:`Hello World`
4652

4753
![](./images/hello-world.png)
54+
55+
## 参考
56+
57+
- <https://www.expressjs.com.cn/>
58+
- <https://www.npmjs.com/package/express>

code/node/express/apps/quick-start/images/hello-world.png renamed to code/express/apps/quick-start-demo/images/hello-world.png

File renamed without changes.

code/node/express/apps/quick-start/images/pwd-info.png renamed to code/express/apps/quick-start-demo/images/pwd-info.png

File renamed without changes.
File renamed without changes.

code/node/express/apps/quick-start/package-lock.json renamed to code/express/apps/quick-start-demo/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/node/express/apps/quick-start/package.json renamed to code/express/apps/quick-start-demo/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
2-
"name": "quick-start",
2+
"name": "quick-start-demo",
33
"version": "0.0.1",
44
"description": "express框架快速开始演示demo",
5-
"main": "app.js",
65
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
6+
"dev": "node app.js"
7+
},
8+
"author": {
9+
"name": "微信公众号:储凡",
10+
"email": "fairy_vip@2925.com"
811
},
9-
"author": "",
1012
"license": "MIT",
1113
"dependencies": {
1214
"express": "^4.18.2"
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Express框架模板项目演示Demo
2+
3+
`Express`框架提供了应用程序生成器,就是预先约定一些`Express`框架项目的开发规范,例如:`目录约束``路由约束`...
4+
5+
通过应用生成器工具 `express-generator`模块 可以快速创建一个应用的结构,可以在此基础上形成基于`Express`框架的模板项目
6+
7+
## 安装依赖
8+
9+
Node版本大于8.2.0或者更高时,可以使用npx命令来运行Express模板生成项目,执行命令:
10+
11+
```bash
12+
npx express-generator
13+
```
14+
15+
也可全局安装`express-generator`模块,执行命令:
16+
17+
```bash
18+
npm install -g express-generator
19+
```
20+
21+
安装完成后,执行命令:
22+
23+
```bash
24+
# 列出可用命令参数
25+
express -h
26+
```
27+
28+
![可用命令参数](./images/express-cmd.png)
29+
30+
## 创建项目
31+
32+
在了解`express`命令的基本可用参数后,可以直接创建名为`template-demo`模版项目,执行命令:
33+
34+
```bash
35+
# 创建模板项目,使用ejs模板引擎
36+
express --view=ejs template-demo
37+
```
38+
39+
对于纯后端的应用,可以使用`--no-view`参数,创建不带模板渲染引擎的模板项目,执行命令:
40+
41+
```bash
42+
# 创建模板项目,不试用渲染引擎
43+
express --no-view template-demo
44+
```
45+
46+
可以清晰地看见新创建了`template-demo`项目:
47+
48+
![](../images/express-create.png)
49+
50+
## 目录结构
51+
52+
注意下项目中的目录结构:
53+
54+
- public目录: 存放前端静态资源,例如:js代码、css代码
55+
- view目录:存放前端页面,在前后端混合开发中,该目录可以用渲染引擎来接受后端数据
56+
- routes目录:后端服务路由,一般用来约定restFul接口
57+
- app.js:项目的入口文件,Node项目一般以`index.js``app.js`作为入口文件,类似与Java开发中的`main.java`文件
58+
- package.json:项目信息、依赖包管理配置文件,可以在里面声明`项目信息``开发脚本``依赖版本`等信息
59+
- bin:这里是框架约定的入口文件,实际开发中可以将该逻辑整合到`app.js`
60+
61+
## 使用项目
62+
63+
```bash
64+
## 下载依赖
65+
npm ci
66+
67+
## 启动项目
68+
npm run dev
69+
70+
# 查看端口
71+
lsof -i:3000
72+
```
73+
74+
当发现端口3000被监听时,可以在浏览器中打开地址:<http://localhost:3000>,查看Express模板项目的运行情况:
75+
76+
![](./images/express-running.png)
77+
78+
## 参考
79+
80+
- <https://www.npmjs.com/package/express-generator>
81+
- <https://www.expressjs.com.cn/starter/generator.html>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const createError = require('http-errors')
2+
const express = require('express')
3+
const path = require('path')
4+
const cookieParser = require('cookie-parser')
5+
const logger = require('morgan')
6+
const http = require('http')
7+
const indexRouter = require('./routes/index')
8+
const usersRouter = require('./routes/users')
9+
const app = express()
10+
const server = http.createServer(app)
11+
12+
// 配置渲染引擎
13+
app.set('views', path.join(__dirname, 'views'))
14+
app.set('view engine', 'ejs')
15+
16+
app.use(logger('dev'))
17+
app.use(express.json())
18+
app.use(express.urlencoded({ extended: false }))
19+
app.use(cookieParser())
20+
app.use(express.static(path.join(__dirname, 'public')))
21+
22+
app.use('/', indexRouter)
23+
app.use('/users', usersRouter)
24+
25+
/**
26+
* 访问不存在的路由,直接抛出 404 错误
27+
*/
28+
app.use(function(req, res, next) {
29+
next(createError(404))
30+
})
31+
32+
/**
33+
* 错误处理中间件
34+
*/
35+
app.use(function(err, req, res, next) {
36+
res.locals.message = err.message
37+
res.locals.error = req.app.get('env') === 'development' ? err : {}
38+
39+
// 渲染异常页面
40+
res.status(err.status || 500)
41+
res.render('error')
42+
})
43+
44+
45+
// 启动服务,监听端口:3000
46+
server.listen(3000)
47+
server.on('error', error => {
48+
if (error.syscall !== 'listen') {
49+
throw error
50+
}
51+
52+
const addr = server.address()
53+
const bind = typeof addr === 'string'
54+
? 'pipe ' + addr
55+
: 'port ' + addr.port
56+
57+
58+
// 分类输出错误信息
59+
switch (error.code) {
60+
case 'EACCES':
61+
console.error(bind + ' requires elevated privileges')
62+
process.exit(1)
63+
break
64+
case 'EADDRINUSE':
65+
console.error(bind + ' is already in use')
66+
process.exit(1)
67+
break
68+
default:
69+
throw error
70+
}
71+
})
72+
73+
/**
74+
* 监听服务启动
75+
*/
76+
server.on('listening', () => {
77+
const addr = server.address()
78+
const bind = typeof addr === 'string'
79+
? 'pipe ' + addr
80+
: 'port ' + addr.port
81+
82+
console.log('listening on:', bind)
83+
})
143 KB
Loading

0 commit comments

Comments
 (0)