Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ng/Express API Route Handler Stops Working

In my Angular/Express.js app, I have a post method in my api.service.ts file:

  post(data: any, endpointUrl: string): Observable<T> {
    console.log("REACHED POST METHOD")
    return this.http.post<T>(`${this.apiUrl}/${endpointUrl}`, JSON.stringify(data), this.httpOptions)
  }

Which sends a post request to a route handler in my index.js file. Here are the contents of index.js:

  import express from 'express';
  import bodyParser from 'body-parser';
  import cors from 'cors';

  const app = express();
  const port = 3000;

  app.use(cors());
  app.use(bodyParser.json());

  app.post('/submit', (req, res) => {
    console.log("REACHED ROUTE HANDLER")
  });

  app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
  });

A button triggers this API call. Both the POST method and the route handler work as they should at first, however, after the tenth time pressing the button, the POST method still runs, but the route handler is not called.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there an inherent limit to the number of times I can call an endpoint? I greatly simplified the POST method and route handler for debugging purposes, and I’ve tried to implement try/catch to catch possible errors, to no avail. I also logged each argument in the POST method return statement to make sure there weren’t any undefined items.

>Solution :

On the server the route handler never actually finishes the response, so it hangs, and the browser limits further requests.

So, you actually need to end the response, for example:

app.post('/submit', (req, res) => {
    console.log("REACHED ROUTE HANDLER");

    // end the request
    res.json(null);
    // or maybe better send something meaningful
    // res.json({success:true, message:'ok'});
});

see:

Response methods

(…) If none of these methods are called from a route handler, the client
request will be left hanging

https://expressjs.com/en/guide/routing.html#response-methods

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading