項目整體流程:
①建數(shù)據(jù)庫、建表?
②初始化服務端項目結構?
?npm init -y npm i express?
③先啟動一個web服務器
app.js
const express = require('express')
?const router = require ('./router')
?//在Express 獲取表單 POST 請求體數(shù)據(jù) npm install --save body-parser
?const bodyParser = require('body-parser')
?const app = express()
?// 把路由應用到 app 中
?app.use(bodyParser.urlencoded({ extended:false }))
?app.use(bodyParser.json())
?app.use(router)
?app.listen(3000,()=>{
?console.log('App is running at port 3000.')
?console.log('Please visit http://127.0.0.1:3000/')
?})
router.js
const express = require('express')
?const router = express.Router()
?const userContoller = require('./controllers/user')
?router
?????.get('/users',userContoller.query)
?????.post('/users',userContoller.new)
?????.patch('/users/id',userContoller.update)
?????.delete('/users/id',userContoller.delete)
user.js
const moment = require('moment') //最強大js時間處理函數(shù)包 npm i moment
?const md5 = require('blueimp-md5') //md5加密算法 首先 在控制臺 npm i blueimp-md5
?exports.query = (req, res, next) =>{ }
?exports.new = (req, res, next) =>{
?const body = req.body
?// 將新建用戶信息插入到users表 mysql設置默認值讓創(chuàng)建時間等于系統(tǒng)時間(百度)
?const sqlStr =
?????????`INSERT INTO 'users'(username, password, email, avater, gender , nickname, ????????create_time, modify_time)
?????????VALUES( '${body.username}',
?????????????????????????'md5( md5( ${body.password} ) )',
?????????????????????????'default-avatar.png',
?????????????????????????'${moment().format('YYYY-MM-DD hh:mm:ss')}', //利用moment獲取當前時間 ????????????????????????'${moment().format('YYYY-MM-DD hh:mm:ss')}', //利用moment獲取當前時間? ? ? ? ? ) `
?// 連接數(shù)據(jù)庫 去npmjs.com 搜mysql 連接代碼
?}
?exports.update = (req, res, next) =>{
?}
?exports.delete = (req, res, next) =>{
?}
④設計接口(數(shù)據(jù)接口路由)
4.1? RESTful ( 接口設計規(guī)范 )? ?
? http://www.ruanyifeng.com/blog/2014/05/restful_api.html
4.1.1 獲取所有用戶? ? ? /users get 返回整個數(shù)組
4.1.2 添加用戶 /users? ? ? post提交 請求體:{username:? ? , password: , email:} 返回整個數(shù)組
4.1.3 修改用戶 /users/id? patch? ? ? 返回整個數(shù)組
4.1.4 刪除用戶 /users/id delete? ? 返回空數(shù)組
用戶登錄成功以后記錄session?
A. npm? i? express-session? ? 去百度搜它的配置使用? 在app.js進行配置