json - Nodejs page freezes after multple requests to Custom API -
the problem have application works when submit 1 one when press submit button multiple times freezes , after time (about 1000.000 ms) returns last request in console , jade page. submit button returns post form , sends same page . button refreshing page. important page returns (json) post page , there other json request sends api(and returns same page )
app.js
var options_search = { hostname: 'host', path: 'path', method: 'post', headers: { 'content-type': 'application/json', 'content-length': json request .length, } }; app.post('/result',function(req,response){ var keyword=req.body.user; global.objtojson ={ keyword }; response.write(json.stringify(global.objtojson)); console.log("test " +json.stringify(global.objtojson) ); }); app.get('/search', function(req, res) { var req = http.request(options_search, (resb) => { var buffer_search = ""; resb.setencoding('utf8'); resb.on('data', (chunks) => { buffer_search += chunks; }); resb.on('end', () => { res.render('search',{ title: 'search', value_search: buffer_search, search_test: [json.stringify(global.objtojson) ] }); }); }); //res.redirect("/search"); req.write(search); }); search.jade
doctype html html head script(src='//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js') script. $(document).ready(function(){ var user,pass; $("#submit").click(function(){ user=$("#user").val(); pass=$("#password").val(); $.post("http://localhost:3000/result",{user: user}, function(data){ if(data==='done') { alert("login success"); } }); }); }); input#user(type='text', size='40') br input#submit(type='button', value='submit',onclick="location.href='search'")
in result route using underlying http .write() method respond client. not end connection, stay open expecting more things written client.
if sending string should use .send() write string http stream , end response.
you may want consider not stringifying object json string , using .json() instead. line of code
response.write(json.stringify(global.objtojson)); becomes
response.json(global.objtojson);
Comments
Post a Comment