express请求接口:
1,设置中间件,borderparser进行响应request的http请求post请求
2.设置不同的路由请求不同的http
3.通过模版传参,动态传数据
用法:
第一:先引入express
const express=require('express')
const app=express()
读取文件:const fs=require('fs')
第二:实例化express
var bodyParser=require('body-parser')
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
第三:指定端口
app.listen(8080)
第四:请求接口
app.get('/hotMusic',function(req,res){
fs.readFile('../static/hotDatas.json',(err,data)=>{
if(err){
res.send('文件读取失败')
}else{
if(JSON.parse(data).length=0){
res.send('暂无数据')
}else{
res.send(JSON.parse(data))
}
}
}
})
在界面上用的时候,这样写:
methods:{
getData(){
这个hotMusic要和自己写的接口的名字一致
this.$axios.get('apis/hotMusic').then(res=>{
console.log(res)
})
}
}