Home > Software engineering >  Heroku react deploy
Heroku react deploy

Time:12-26

So the build runs, but it obviously is not using the right package.json file since the only dependency it gets is npm. When i got to my deployed website I get the default Application error from Heroku. These are the build logs:

Logs

This is how my file layout looks like: Visual Studio

This is the server.js file:

const express = require('express')
const mongoose = require('mongoose')
const PORT = process.env.PORT || 3000
const jwtAuth = require("./middleware/jwtAuth")
require("dotenv").config()
const cors = require('cors');
const authRoutes = require('./routes/auth')
const orderRoutes = require('./routes/order')
const axios = require('axios')

const corsOptions = {
  origin: 'http://localhost:3000',
}

const app = express()
//this will convert the request into json, since node doesn't accept json by default
app.use(express.json());
//this will enable cors on localhost
app.use(cors(corsOptions));


app.use('/api/auth', authRoutes)
app.use('/portfolio', jwtAuth)
app.use('/api/order', orderRoutes)

app.get('/secret', jwtAuth, (req, res) => {
  res.send('Secret Hello World!')
})
//fetch testing
app.use('/stock', jwtAuth, function(req, res, next) {
  const apikey = "M7DSRJECMBCEEWGF";
  const ticker = ['MSFT']

  let completed = 0;
  const results = [];
  console.log(ticker);
  for (let i = 0; i < ticker.length; i  ) {
    const oneTicker = ticker[i];

    axios.get(
        `https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=${ticker}&apikey=${apikey}`
      )
    .then((response) => {
      completed  = 1;

      results.push(response.data);
      if (completed === ticker.length) {
        //All ticker have finished their response
        console.log("completed");

        res.send({
          success: true,
          message: "Ticker info",
          results,
        });
      }

      console.log(ticker);
    })
    .catch((e) => {
      console.error(e);
    });
  }
})

app.get('/auth', jwtAuth, (req, res) => {
  res.status(200).send("Welcome home, cowboy")
})

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.get('/secret', jwtAuth, (req, res) => {
  res.send('Secret Hello World!')
})

app.get('*', (req, res) => {
  res.send('This route does not exist')
})

mongoose
  .connect(process.env.MONGODB_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => {
    app.listen(PORT, () => console.log(`Server started on PORT ${PORT}`))
  })
  .catch((err) => {
    console.log(err)
    process.exit(1)
  })

Heeelp, I really want to understand how deployment on heroku works! :)

CodePudding user response:

You have mongoose defined in your devDependencies, which do get installed during the slug creation phase, but they get removed before the app is actually deployed, which is documented here.

By default, Heroku will install all dependencies listed in package.json under dependencies and devDependencies.

After running the installation and build steps Heroku will strip out the packages declared under devDependencies before deploying the application.

With this information, you can choose to either move mongoose to dependencies - which is probably what you want.

If you want to keep it under devDependencies though, you can have Heroku keep them in the built slug by updating your configuration. I don't recommend this though, unless you have a good reason to keep it as a development dependency.

heroku config:set NPM_CONFIG_PRODUCTION=false YARN_PRODUCTION=false
  • Related