mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
/q json response, searching with "" subj if there is no result
This commit is contained in:
parent
bf86b91172
commit
a1e99c53ae
3 changed files with 29 additions and 8 deletions
|
@ -248,32 +248,46 @@ app.route('/badtestsender').post(function (req, res, next) {
|
|||
|
||||
app.post('/isAdding', function (req, res) {
|
||||
logger.LogReq(req)
|
||||
// automatically saves to dataFile every n write
|
||||
let result = actions.ProcessIncomingRequest(req.body.datatoadd, data)
|
||||
res.json({
|
||||
success: result !== -1,
|
||||
newQuestions: result
|
||||
})
|
||||
utils.WriteBackup() // TODO: write every n times / after n minutes
|
||||
})
|
||||
|
||||
app.get('/q', function (req, res) {
|
||||
logger.LogReq(req)
|
||||
if (Object.keys(req.query).length === 0) {
|
||||
res.send('ask something!')
|
||||
res.json({
|
||||
message: `ask something! ?q=[question]&subj=[subject]&data=[question data]. 'subj' is optimal for faster result`,
|
||||
result: [],
|
||||
recievedData: JSON.stringify(req.query),
|
||||
success: false
|
||||
})
|
||||
} else {
|
||||
if (req.query.q) {
|
||||
if (req.query.q && req.query.data) {
|
||||
let subj = req.query.subj || ''
|
||||
let question = decodeURIComponent(req.query.q)
|
||||
let recData = {}
|
||||
try {
|
||||
recData = JSON.parse(req.query.data)
|
||||
} catch (e) {
|
||||
console.log(e) // TODO: normal error logging
|
||||
logger.Log(`Unable to parse recieved question data! '${req.query.data}'`, logger.GetColor('redbg'))
|
||||
}
|
||||
let r = data.Search(decodeURIComponent(req.query.q), subj, recData)
|
||||
let r = data.Search(question, subj, recData)
|
||||
|
||||
res.json(r)
|
||||
res.json({
|
||||
result: r,
|
||||
success: true
|
||||
})
|
||||
} else {
|
||||
res.send('no key "q" in query' + JSON.stringify(req.query))
|
||||
res.json({
|
||||
message: `Invalid question :(`,
|
||||
result: [],
|
||||
recievedData: JSON.stringify(req.query),
|
||||
success: false
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue