在api文件夹中,我有3个js文件(calls.js,meetings.js,statistics.js)。所有文件看起来都一样:
const express = require('express')
const router = express.Router()
var knex = require('knex')({
client: 'mysql',
connection: {
host : 'host',
user : 'user',
password : 'password',
database : 'database'
}
});
***knex functions***
module.exports = router
所有这些都以相同的方式添加到index.js文件中:
app.use('/api/calls', require('./routes/api/calls'))
app.use('/api/statistics', require('./routes/api/statistics'))
app.use('/api/meetings', require('./routes/api/meetings'))
基于种种原因,Meetings.js都会引起麻烦:
1.如果从Meetings.js中删除所有knex函数-一切正常;
2.如果从index.js删除行app.use('/api/meetings', require('./routes/api/meetings'))
-一切正常;
否则浏览器会说:“无法访问此站点... ERR_CONNECTION_TIMED_OUT”
Meetings.js的完整代码:
const express = require('express')
const router = express.Router()
var knex = require('knex')({
client: 'mysql',
connection: {
host : 'host',
user : 'user',
password : 'password',
database : 'database'
}
});
app.post('/newMeetingDay', (req, res) => {
const city = req.body.city
const date = req.body.date
const scheduled = req.body.scheduled
const happened = req.body.happened
const adults = req.body.adults
const children = req.body.children
if (!city || !date || !scheduled || !happened || !adults || !children){
return res.json({Error: 'details are missing'})
}
knex('MeetingDays').insert({City: city, Date: date, Scheduled: scheduled, Happened: happened,
Adults: adults, Children: children}).then(data => {
return res.json({Done: 'new meeting day saved'})
}).catch(err => {
return res.json({Error: err})
})
})
module.exports = router
有人知道什么可能是问题吗?
答案 0 :(得分:0)
发现问题:在meetings.js
中,该功能应为router.post
,而不是app.post