简体   繁体   中英

Express js, GET request to the same route for the second time always pending

I am developing an express application in which I need to render a landing page for '/' route. This page has a text box and a button. When user adds text to textbox and hits submit button I need to make request to same url but with text added as its query parameter. This request will render a new page whose contents depend upon query text.

Now what is happening is when user enters text into textbox and presses button, form makes GET request and route looks something like http://localhost:3000/?q=abc . I can see this request as pending in network tab of chrome developer tools but this request never reaches server. I cannot see any request for /?q=abc in server console.

If I restart server and then open http://localhost:3000/?q=abc , proper contents get loaded in browser. But if I change url back to http://localhost:3000 , I can see pending request in network tab and this request never reaches server.

My code looks something like:

app.get('/', function(req, res, next) {
  if(req.query.q) 
    res.render('results.ejs');
  else
    res.render('landing.ejs')
});

Any idea why no call to server is being made? I have spent whole day trying to figure out reason for this behavior. Any help will be greatly appreciated.

Have you tried inserting temporary middleware to see where exactly the request gets stuck in your middleware/route chain? It sounds like there is a middleware somewhere that is not calling next() in some situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM