Home > front end >  Heroku successfully deplots node.js react.js project but I got an "Application error" pa
Heroku successfully deplots node.js react.js project but I got an "Application error" pa

Time:02-10

Heroku says it successfully deployed my app but when I visit the page it says "application error" I tested my app in visual studio and it works fine there. Here is my node.js code

//code based off of:
// https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9

const express = require('express');
const request = require('request');

const app = express();
const apiKey = 'api-key';
const PORT = process.env.PORT || 5000;

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  next();
});

if(process.env.NODE_ENV === 'production'){
    app.use(express.static('build'));
    app.get('*', (req, res) =>{
        req.sendFile(path.resolve(__dirname, 'build','index.html'))
    })
}

app.get('/getCoinInfo', (req, res) => {
  console.log('The request has been received!!')
  request(
    { url: 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY='   apiKey},
    (error, response, body) => {
      if (error || response.statusCode !== 200) {
        return res.status(500).json({ type: 'error', message: err.message });
      }

      res.json(JSON.parse(body));
    }
  )
});


app.listen(PORT, () => console.log(`listening on ${PORT}`));

Here is also my Json File

{
  "name": "cryptoreactv2",
  "version": "0.1.0",
  "private": true,
  "proxy": "http://localhost:5000",
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.2",
    "@testing-library/react": "^12.1.2",
    "@testing-library/user-event": "^13.5.0",
    "cors": "^2.8.5",
    "express": "^4.17.2",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "5.0.0",
    "request": "^2.88.2",
    "serve": "^13.0.2",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "startOLD": "node app",
    "startOLD": "react-scripts start",
    "dev": "react-scripts start",
    "start": "serve -s build",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "heroku-postbuild": "npm run build"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "nodemon": "^2.0.15"
  }
}

I tried looking at different tutorials but nothing appears to work. Again, my app works in visual studio but not in Heroku.

CodePudding user response:

After taking a short break, I realized that I had a Procfile that was not needed, so I deleted in and pushed the new changes onto Heroku.

  •  Tags:  
  • Related