Star-sparkles
Star-sparkles

Reputation: 1

What is causing a 404 error when I start a Heroku server?

This is my error log:

2024-05-27T18:13:44.000000+00:00 app[api]: Build succeeded
2024-05-27T18:13:52.740529+00:00 heroku[web.1]: Starting process with command `node api/server.js`
2024-05-27T18:13:53.664442+00:00 app[web.1]: Server is running on port number 50973
2024-05-27T18:13:53.923152+00:00 heroku[web.1]: State changed from starting to up
2024-05-27T18:13:58.773592+00:00 heroku[router]: at=info method=GET path="/" host=madmacros-5940fe8a89b0.herokuapp.com request_id=d89ef726-878c-4ec6-a107-67a44484282b fwd="173.89.45.96" dyno=web.1 connect=0ms service=6ms status=404 bytes=415 protocol=https 
2024-05-27T18:14:24.331747+00:00 heroku[router]: at=info method=GET path="//submit/?cals=500&protein=30&food=Chipotle" host=madmacros-5940fe8a89b0.herokuapp.com request_id=84284955-7590-4630-9e3b-7936298339bc fwd="173.89.45.96" dyno=web.1 connect=0ms service=2ms status=404 bytes=423 protocol=https

I've put the node version in the engines in the package.json. I already have const port = process.env.PORT || 5000; inside my server.js. These were all things that were suggested but I still can't find the solution. Note that my server works great locally!

this is my server.js:

const csv = require('csv-parser') //node package to parse csv
const fs = require('fs') //file system package
const cors = require('cors');
const express = require('express'); //express 
const app = express();
app.use(cors());

const port = process.env.PORT || 5000; 


app.get('/submit',  async (req, res) => {
  try {
    //prints out the request query parameters
    console.log("protein: " + req.query.protein)
    console.log("cals: " + req.query.cals)
    console.log("food: " + req.query.food)
    let array;
    let csvName;

    switch (req.query.food){
      case "Panera Bread": 
        csvName = "api\\panera-bread.csv";
        break;
      case "Chic Fil A":
        csvName = "api\\chick-fil-a.csv";
        break;
      case "Chipotle":
        csvName = "api\\chipotle.csv" 
        break;
      case "Ihop":
        csvName = "api\\ihop.csv"
        break;
      case "Jamba":
        csvName ="api\\jamba.csv"
        break;
      case "Shake Shack":
        csvName = "api\\shake-shack.csv"
        break;
      case "Halal Guys":
        csvName = "api\\the-halal-guys.csv"
        break;
    }
    array = await parsecsvFile(csvName, Number(req.query.cals), Number(req.query.protein))

    res.type('json')
    res.json(array)
   }
   catch (error) {
    console.error("Error processing CSV file:", error);
    res.status(500).send("Failed to process CSV file");
  }
  
 
 } 
  
  );


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

//exports app for Vercel 
  module.exports = app;

//Function that will parse a given csv for protein, cals, and name
function parsecsvFile(csvPath, cals, protein){
  return new Promise((resolve, reject) => {
    const results = [];
    fs.createReadStream(csvPath)
  .pipe(csv({
    mapHeaders: ({ header}) => {
      // Column headers to keep 
      if (header === 'Name' || header === 'Serving Size Description' || header === 'Calories' ||header === 'Protein (g)') {
        return header;
      }
      return null;  // will ignore header if it's not in my if statement
    }
  }))
  .on('data', (data) => {
    //pushes only the objects from data that matches the cals and protein into the array
    //Number() is used to format the string into a number
    //note that to access the Protein we have to use quotes because of the spaces 
    
    if (Number(data.Calories) <= cals && Number(data['Protein (g)']) >= protein) {
   
      results.push(data); 
    }}

) 
.on('end',()=>{
  resolve(results);
})
  
  .on('error', (error) => {
    reject(error);
});
});
}

and this is my Procfile: web: node api/server.js

Upvotes: 0

Views: 37

Answers (0)

Related Questions