首页 > 解决方案 > 在使用 express 的 axios 请求后重新启动服务器

问题描述

我的问题是,我的项目服务器在使用 express 的 axios 请求后重新启动。我可以在 chrome 的控制台中看到http://localhost:3000/__webpack_hmr 200 OK。

当我向数据添加词汇或删除它时,服务器会重新启动。这是vue客户端代码。(github链接到文件

methods: {
    addVocabulary () {
      var vm = this
      this.$validator.validateAll().then((result) => {
        if (result) {
          axios.post('/api/vc/add', {
            vocabulary: this.vocabulary
          }).then(function (response) {
            vm.vocabularies = response.data
          }).catch(function (error) {
            console.log(error)
          })
          this.vocabulary = ''
        } else {
          console.log('Not valid')
        }
      })
    },
    remove (id) {
      var vm = this
      axios({
        method: 'delete',
        url: '/api/vc/remove',
        data: {id: id},
        headers: {'Content-Type': 'application/json'}
      }).then(function (response) {
        vm.vocabularies = response.data
      }).catch(function (error) {
        console.log(error)
      })
    }
  }

这是服务器端代码。文件链接

router.post('/vc/add', function (req, res, next) {
  if (!req.body) return res.sendStatus(400)
  let vocabulary = req.body.vocabulary
  let objVocabulary = {}
  objVocabulary.id = 10
  objVocabulary.content = vocabulary
  vocabularies.push(objVocabulary)

  fse.outputJson(file, vocabularies, err=>{
    console.log(err);
    fse.readJson(file, (err, data) =>{
        res.json(data);
    })
  })
})

/* DELETE remove voca */
router.delete('/vc/remove', (req, res, next) =>{
  if (!req.body) return res.sendStatus(400)
  let id = req.body.id
  var index = vocabularies.findIndex(voca => voca.id === id);
  vocabularies.splice(index,1);

  fse.outputJson(file, vocabularies, err=>{
    console.log(err);
    fse.readJson(file, (err, data) =>{
        res.json(data);
    })
  })

})

这是我的项目链接到github,我不知道为什么..

标签: javascript

解决方案


我看到你正在使用backpack.

他们的文档引用:实时重新加载(保存、添加/删除文件等)

因此,如果您在服务器代码中操作的 JSON 文件位于背包监视的文件夹中,热模块重新加载将启动并重新启动服务器。

解决方案:将 json 移动到其他目录 --> 无论如何,您的数据库不应该是源代码的一部分。


推荐阅读