作項目的時候會不會由於沒有數據而煩惱?沒有後端給接口而感到舉步維艱?接下來,這些都不是問題,咱們全均可以本身作。javascript
這三個分別是node的框架express,解析post請求數據的body-parser中間件,鏈接數據庫的中間件mysql。前端
//index.js
const express = require('express')
const bodyParser = require('body-parser')
const mysql = require('mysql')
let db = mysql.createPool({ //這裏建議使用createPool,這樣可使用多個服務,默認10個,能夠本身設置
host: 'localhost',
user: 'root',
password: 'root',
database: 'property_system'
})
let app = express() //使用express啓動一個服務,監聽3000端口
app.listen(3000, () => {
console.log('正在運行...')
})
複製代碼
不瞭解mysql中間件的配置項的戳這裏:npm.taobao.org/package/mys…vue
app.use(bodyParser.urlencoded({ //使用body-parser中間件
extended: false
}))
app.use(bodyParser.json())
複製代碼
接下來,只要在server文件夾下,啓動一個命令行,輸入node index。這樣一個簡單的node服務就啓動起來了。java
app.post('/api/login', (req, res) => {
console.log(req.body)
req.db.query(`SELECT * FROM admin_table WHERE admin='${req.body.admin}'`, (err, data) => {
if (err) {
console.log(err)
res.status(500).send('數據庫有問題了')
} else {
console.log(data)
if (data.length > 0 && req.body.password === data[0].password) {
console.log(`驗證完畢,權限爲${data[0].power_id}`)
res.send({
power_id: data[0].power_id
})
} else {
console.log('帳號密碼有誤')
res.send('帳號/密碼錯誤')
}
}
})
})
複製代碼
這裏咱們建立好了一個post請求的接口,路徑是/api/login,這是一個簡單的登陸驗證的接口,會根據前臺請求的數據判斷數據庫是否有對應的數據,若是有會返回一個權限等級,沒有會提示帳號/密碼錯誤。node
proxyTable: {
'/api': { //使用/api代理對3000端口服務的請求
target: 'http://localhost:3000',
changeOrigin: true,
pathRewrite: {
'^/api': '/api'
}
},
},
複製代碼
import axios from 'axios'
Vue.prototype.$http = axios
複製代碼
go_login () {
console.log(this.account)
this.$http.post('/api/login', {
admin: this.account,
password: this.password
}).then(res => {
console.log(res)
if (res.data.power_id) {
this.$global.power_limit = res.data.power_id
this.$router.push('/home')
} else {
console.log('錯誤')
this.$notify({
message: '帳號/密碼錯誤',
type: 'warning'
});
}
}).catch(rej => {
this.$notify({
message: rej,
type: 'warning'
});
})
},
複製代碼
如何請求接口對你們都是小意思了,這裏就再也不贅述。 好了,到這裏,恭喜你,已經邁出了前端全棧的第一步了mysql
Navicat破解版:連接:pan.baidu.com/s/1vaHpaa4l… 提取碼:h6zt 複製這段內容後打開百度網盤手機App,操做更方便哦ios
mysql:連接:pan.baidu.com/s/1TTp6vo-P… 提取碼:ptmy 複製這段內容後打開百度網盤手機App,操做更方便哦sql