初识Express

139 阅读3分钟

Express

起步

安装

npm install --save express

hello word

const express = require('express');

const app = express();

app.get('/', function(req, res){
	res.send('hello word');
})

app.listen(3000, function(){
	console.log('Express app is running');
})

基本路由

路由器

  • 请求方法

  • 请求路径

  • 请求处理函数

get:

//当你以GET方法请求 / 的时候,执行对应的处理函数

app.get('/', function(req, res){
	res.send('hello word');
})

post:

//当你以POST方法请求 / 的时候,执行对应的处理函数

app.post('/', function(req, res){
	res.send('hello word post');
})

静态服务

//  /public 资源   http://127.0.0.1:3000/post.html
app.use(express.static('public'))

//  /public/xxx   http://127.0.0.1:3000/public/post.html
app.use('/public/', express.static('public'))


//  /static/xxx   http://127.0.0.1:3000/static/post.html
app.use('/static/', express.static('public'))

//
app.use('/static', express.static(path.join(__dirname, 'public')))

在Express中配置使用art-template模板引擎

art-template官方文档

安装:

npm install --save art-tempalte
npm install --save express-art-template

配置:

app.engine('art', require('express-art-template'));

使用:

app.get('/', function(req, res){
	//express默认会去项目中的views目录中找index.html
  res.render('index.html', {
  	title:'hello word'
  })
})

在Express获取表单POST请求体数据

(body-parser 已经弃用)

配置:(要写在 app.use(router)路由容器挂载到路由容器 之前)

app.use(express.urlencoded({ extended: false }))

使用:

app.post('/', function(req, res){
	console.log(req.body)
})

\


CRUD案例

模块化思想

  • 模块职责要单一

起步

  • 初始化
  • 安装依赖
  • 模板处理

路由设计

请求方法请求路径get参数post参数备注
GET/students渲染首页
GET/students/new渲染添加学生页面
POST/students/newname,age,gender处理添加学生请求
GET/students/editid渲染编辑页面
POST/students/editid,name,age,gender处理编辑请求
GET/students/deleteid处理删除请求

提取路由模块

router.js

/**
 * router.js 路由模块
 * 职责:
 *   处理路由
 *   根据不同的请求方法+请求路径设置具体的请求处理函数
 * 模块职责要单一,不要乱写
 * 我们划分模块的目的就是为了增强项目代码的可维护性
 * 提升开发效率
 */ 


const fs = require('fs');

// Express提供了一种方式来包装路由
const express = require('express');

// 1,创建一个路由容器
const router = express.Router();


// 2,把路由挂载到router路由容器中
router.get('/students/', function(req, res){
    
})

router.get('/students/new', function(req, res){ 

})

router.post('/students/new', function(req, res){
  
})
router.get('/students/edit', function(req, res){

})
router.post('/students/edit', function(req, res){

})
router.get('/students/delete', function(req, res){

})


// 3.把router导出
module.exports = router

app.js

var router = reuqire('./router');

//挂载路由
app.use(router);

设计操作数据的API文件模块

/**
 * student.js
 * 数据操作文件模块
 * 职责:操作文件中的数据,只处理数据,不关心业务
 *
 * 这里才是我们学习 Node 的精华部分:奥义之所在
 * 封装异步 API
 */

// 引包
var fs = require('fs');
var dbPath = './db.json'

/**
 * callback中的参数
 *      第一个是err
 *          成功是null
 *          错误是错误对象
 *      第二个是 结果
 *          成功是 数组
 *          错误是 undefind
 * 
 * return []
 */


// 封装这个方法的目的:如果还有其他的页面需要students数据,直接调用students模块调用find方法
/**
 * 查询所有学生数据
 */
exports.find = function(callback){
    fs.readFile(dbPath, 'utf8', function(err, data){

        if(err){
            return callback(err)
        }

        callback(null, JSON.parse(data).students)
    })

}

/**
 * 添加保存学生
 */

exports.save = function(){
    
}


/**
 * 更新学生
 */

exports.update = function(){

}


/**
 * 删除学生
 */

exports.delete = function(){

}

自己编写的步骤

  1. 处理模板
  2. 配置开放静态资源
  3. 配置模板引擎
  4. 简单路由:/students渲染静态页出来
  5. 路由设计
  6. 提取路由模块
  7. 由于接下来的一系列业务操作都需要处理文件数据,所以我们要封装student.js
  8. 先写好student.js的文件结构
    • 查询所有学生列表的API find
    • findById
    • save
    • updateById
    • deleteById
  9. 实现具体功能
    • 通过路由收到请求
    • 接受请求中的数据(get,post)
      • req.query
      • req.body
    • 调用数据操做API处理数据
    • 根据操作结果给客户端发送响应