javascript - 在使用 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,我不知道为什么..
解决方案
我看到你正在使用backpack
.
他们的文档引用:实时重新加载(保存、添加/删除文件等)
因此,如果您在服务器代码中操作的 JSON 文件位于背包监视的文件夹中,热模块重新加载将启动并重新启动服务器。
解决方案:将 json 移动到其他目录 --> 无论如何,您的数据库不应该是源代码的一部分。
推荐阅读
- matlab - 中断代码执行时如何阻止Matlab启动调试器
- typescript - 无法扩展 .d.ts 定义
- python - Django ManyToOne 反向关系允许空值
- .net - UWP 文件权限?
- c - 如何在 c 编程中删除在以下结构中具有成员函数的结构?
- binary - 计算机如何添加负数?
- ubuntu-16.04 - 如何设置 aptdcon 以停止在 Ubuntu 16.04 中询问确认?
- c# - 禁用导航属性 ef-core2.2 的命名约定
- python - Selenium 在 Python 中进入无限循环
- node.js - 在 express 节点 js 中获取查询参数