Commit b2dbcaa4 by Иван Кубота

parse POST json bodies

logging ability
parent a1fad49d
Pipeline #618 failed with stage
#KUS_GENERATOR_BACKEND=https://api.new.local.vkusvill.testin.ru
KUS_GENERATOR_BACKEND=https://api.vkusvill.flexlab.pro
KUS_GENERATOR_PORT=4001
\ No newline at end of file
KUS_GENERATOR_PORT=1001
\ No newline at end of file
......@@ -34,7 +34,7 @@ const print = function(data) {
return tpls.index.replace('$DATA$', data)
};
const api = [require('./src/api/generateRandom'), require('./src/api/crud')].reduce((store, el)=>{
const api = [require('./src/api/generateRandom'),require('./src/api/log'), require('./src/api/crud')].reduce((store, el)=>{
return Object.assign(store, el)
}, {});
const {api2html, api2routes} = require('./src/apiMappers.js');
......
{"type":"ANSWER","date":1591122005507,"data":{"state":{"quizCount":30,"currentChain":0,"rights":1,"maxChain":1,"currentQuestion":2},"answers":{"answers":[{"correct":false,"text":"Маленькой игрушкой","isUserChoice":true,"isCorrect":false,"isInCorrect":true,"type":"radio"},{"correct":false,"text":"Советами психолога","isUserChoice":false,"isCorrect":false,"isInCorrect":false,"type":"radio"},{"correct":true,"text":"Игрой","isUserChoice":false,"isCorrect":false,"isInCorrect":true,"type":"radio"}],"right":false}}}
{"type":"ANSWER","date":1591122006659,"data":{"state":{"quizCount":30,"currentChain":0,"rights":1,"maxChain":1,"currentQuestion":3},"answers":{"answers":[{"correct":false,"text":"Подбор товара","isUserChoice":false,"isCorrect":false,"isInCorrect":false,"type":"radio"},{"correct":true,"text":"Ноль на весах","isUserChoice":false,"isCorrect":false,"isInCorrect":true,"type":"radio"},{"correct":false,"text":"Один раз \"минус 20 гр\"","isUserChoice":true,"isCorrect":false,"isInCorrect":true,"type":"radio"},{"correct":false,"text":"2 раза \"минус 20 грамм\"","isUserChoice":false,"isCorrect":false,"isInCorrect":false,"type":"radio"}],"right":false}}}
var fs = require('fs');
module.exports = {
'/api/log/start/:userID': {
options: {
userID: { required: true, description: 'ID of user', type: Number }
},
method: 'POST',
summary: 'Get list of new cards',
fn: async function(args) {
fs.writeFile('./public/log/'+args.userID+'.jsons', JSON.stringify({
type: 'START', date: +new Date(), data: args.body})+'\n', {'flag':'a'},
function(err) {
console.log(err);
});
return true;
}
},
'/api/log/step/:userID': {
options: {
userID: { required: true, description: 'ID of user', type: Number }
},
method: 'POST',
summary: 'Get list of new cards',
fn: async function(args) {
fs.writeFile('./public/log/'+args.userID+'.jsons', JSON.stringify({
type: 'ANSWER', date: +new Date(), data: args.body})+'\n', {'flag':'a'},
function(err) {
console.log(err);
});
return true;
}
}
};
\ No newline at end of file
......@@ -162,6 +162,31 @@ ${opt.required?'<span class="api-option-required">Required</span>': '<span class
return res.end('Errors:\n' +e.map(e=>'\t'+e).join('\n'))
}
if(req.method === 'POST'){
try{
args.body = await new Promise( function( r, j ){
var body = '';
req.on('data', function (data) {
body += data;
// Too much POST data, kill the connection!
// 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB
if (body.length > 1e6){
req.connection.destroy();
j();
}
});
req.on('end', function () {
r(JSON.parse(body));
});
} )
}catch(e){
args.body = null;
}
}
console.log(api.method, key, JSON.stringify(args));
res.header("Content-Type", "application/json; charset=utf-8");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment