使用express发出axios请求后重新启动服务器

时间:2018-05-04 03:12:22

标签: javascript

我的问题是,我的项目服务器重启后用快速公司发出axios请求。 我可以在Chrome控制台中看到http://localhost:3000/__webpack_hmr 200 OK。

当我向数据添加词汇表或删除它时,服务器重新启动。 这是vue客户端代码。 (github link to file

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)
      })
    }
  }

这是服务器端代码。link to file

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, 我不知道为什么......

1 个答案:

答案 0 :(得分:0)

我看到你正在使用backpack

  

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

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

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

相关问题