/*----------------jsonServer---------*/ /*引入json-server*/const jsonServer = require('json-server') /*搭建一个server*/const apiServer =jsonServer.create() /*将db.json关联到server*/const apiRouter = jsonServer.router('db.json') const middlewares =jsonServer.defaults() apiServer.use(middlewares) apiServer.use(apiRouter) /*监听端口*/apiServer.listen(3000, () =>{ console.log('JSON Server is running') })
在config文件夹的index.js 配置跨域
/*代理配置表,在这里可以配置特定的请求代理到对应的API接口*/ /*下面的例子将代理请求 /api/getNewsList 到 http://localhost:3000/getNewsList*/proxyTable: { '/api': { changeOrigin: true, //如果接口跨域,需要进行这个参数配置 target: 'http://localhost:3000', //接口的域名 pathRewrite: { '^/api': '' //后面可以使重写的新路径,一般不做更改 } } },