Question

Dynamic routing and app platform

Posted July 30, 2021 174 views
ReactDigitalOcean App Platform

Hey there, I’m trying to solve a 404 error that I’m receiving when calling an API I built. It seems that it has to do with App Platform not working with dynamic routing, but I’m looking to see if there’s a solution for this that I’m missing or what not.

The Idea

/api/zip/{{zipcode}}

The Problem
Receiving Cannot GET /12345

Here’s my current configuration:

- github:
    branch: project-name
    deploy_on_push: true
    repo: repo/project-name
  instance_count: 1
  http_port: 3001
  instance_size_slug: basic-xxs
  name: express-server
  run_command: 'node server/index.js'
  routes:
  - path: /api/zip/
  source_dir: /

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @lewisadam1992,

App Platform supports dynamic routing and the ‘Cannot GET /path’ error usually occurs due to the configuration issues. It would be difficult to figure out the exact reason for the failure without looking at the code.

Here are some links that might be helpful:
https://stackoverflow.com/questions/9627441/cannot-get-with-connect-on-node-js
https://stackoverflow.com/questions/50629652/strange-create-react-app-heroku-error-cannot-get

If the issue persists, I’d request you to raise a support ticket to us so that we can have a closer look on the same.

Cheers,
Taha Fatima

  • Hey there, I tried the method that was mentioned above and I wanted to know, if I can use dynamic routing then how do I pass parameters? I’m trying to achieve an API call to USPS, by having the API be called on the route /api/zip/{{zipCode}}, but when I do that it throws a 404, because technically the path doesn’t exist in the route.

    Here is the piece of code i’m referring to:

    app.get('/api/zip/:zipCode', cors(corsOptions), (request, response, next) => {
    
      const zip = request.params.zipCode;
    
      usps.cityStateLookup(zip, (error, result) => {
        response.json(result);
      });
    });
    
    if (process.env.NODE_ENV === 'production') {
      // Express will serve up production assets
      app.use(express.static('client/build'));
    
      // Express serve up index.html file if it doesn't recognize route
      const path = require('path');
      app.get('*', (req, res) => {
        res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
      });
    }
    
    app.listen(PORT, () => {
      console.log("Starting Web Server on port " + PORT);
    })