后台
const express = require('express')
const app = new express()
app.get('/api/news', (req, res) => {
console.log('请求');
res.send('Hello World66666!')
})
app.get('/api/person', (req, res) => {
res.send('me is person')
})
app.get('/get/person', (req, res) => {
res.send('me is get person')
})
app.get('/book', (req, res) => {
res.send('me is book')
})
app.get('/books', (req, res) => {
res.send('me is books')
})
app.listen(3000, () => {
console.log('Server Start');
})
前台配置 proxy
module.exports = {
devServer: {
open: true,
port: 9997,
proxy: {
"/api": {
target: "http://localhost:3000",
changeOrigin: true,
},
"/get": {
target: "http://localhost:3000",
changeOrigin: true
},
"/data": {
target: "http://localhost:3000",
changeOrigin: true,
pathRewrite: {
'^/data': '/'
}
},
}
}
}
请求示例
const { data: news } = await this.$http.get("/api/news");
console.log("news:", news);
this.msg = news;
const apiPerson = await this.$http.get("/api/person");
console.log("apiPerson:", apiPerson);
const getPerson = await this.$http.get("/get/person");
console.log("getPerson:", getPerson);
const book = await this.$http.get("/data/book");
console.log("book: ", book);
const books = await this.$http.get("/data/books");
console.log("books: ", books);
请求结果
