vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > vue express启动

详解vue express启动数据服务

作者:itSeven7

本篇文章主要介绍了vue express启动数据服务,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

这两天学习了一下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/'
  },

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

您可能感兴趣的文章:
阅读全文