一.1.1 Express4.16.3 基于 Node.js 平台,快速、开放、极简的 Web 开发框架
Express 提供精简的基本 Web 应用程序功能,而不会隐藏您了解和青睐的 Node.js 功能。
const http = require('http')
const fs = require('fs')
const app = http.createServer((req, res) => {
switch (req.url) {
case '/':
res.write('home')
res.end('')
break
case '/index.html':
fs.readFile('./pages/index.html', 'utf8', (err, data) => {
res.write(data)
res.end('')
})
break
case '/ .js':
fs.readFile('./app.js', 'utf8', (err, data) => {
res.write(data)
res.end('')
})
break
default:
res.write('404')
res.end('')
}
})
app.listen(3000, () => {
console.log('localhost:3000')
})
1.2使用express框架时访问资源
app.use(express.static(path.join(__dirname, 'public')));
二。RMVP设计模式的实现
//app.js
// 开发接口
app.use('/api/position', positionRouter)
R.>>>>>
// R.>>>>>
const express = require('express')
const positionController = require('../controllers/position') //调用controller层
const router = express.Router()
// 注意,listall不要调用
router.get('/listall', positionController.listall) //和/api/position拼接 post也是一样
router.post('/save', () =>{
res.header('Content-Type', 'application/json; charset=utf-8')//res的表头定义 返回给前端的类型 form是post query是get post json是request payload
})
//后端是不能解析json字符串的 使用bodyparse
module.exports = router