网络编程 
首页 > 网络编程 > 浏览文章

详解vue express启动数据服务

(编辑:jimmy 日期: 2025/4/21 浏览:3 次 )

这两天学习了一下vue和express结合,本文记录一下vue express启动数据服务

记录一下配置

build->dev.sever.js配置

var apiServer = express()
var bodyParser = require('body-parser')
apiServer.use(bodyParser.urlencoded({ extended: true }))
apiServer.use(bodyParser.json())
var apiRouter = express.Router()
var fs = require('fs')
apiRouter.route('/:apiName')
.all(function (req, res) {
 fs.readFile('./db.json', 'utf8', function (err, data) {
  if (err) throw err
  var data = JSON.parse(data)
  if (data[req.params.apiName]) {
   res.json(data[req.params.apiName]) 
  }
  else {
   res.send('no such api name')
  }

 })
})

apiServer.use('/api', apiRouter);
apiServer.listen(port + 1, function (err) {
 if (err) {
  console.log(err)
  return
 }
 console.log('Listening at http://localhost:' + (port + 1) + '\n')
})

config->index.js配置

 proxyTable: {
    '/api/':'http://localhost:8081/'
  },

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:jQuery实现返回顶部按钮和scroll滚动功能[带动画效果]
下一篇:nodeJS(express4.x)+vue(vue-cli)构建前后端分离实例(带跨域)