I have a tiny backend with literally 1 endpoint. It works flawlessly on my local machine. After a cold startup, it takes roughly 4 seconds to return on my local machine. But when I deploy to Heroku, it boots up and loads fine, but if you try to utilize it, it crashes with code H13 which after some research is equivalent to Heroku intentionally crashing it because it took too long to load. From what I can tell it's supposed to happen after 30 seconds of attempting to load, though after timing it, it's closer to about 10 before it crashes. I've also seen 1 potential workaround where they used a timeout function every 3 seconds to check the status of the server, though frankly the code was beyond me. My best guess is that the await OpenseaScraper is taking too long to respond, but a single call to an api shouldn't be that long right?
Here is my code
const express = require('express');
const OpenseaScraper = require('opensea-scraper')
const mongoose = require('mongoose');
require('dotenv').config()
const NFT = require('./NFTModel')
const server = express()
const router = express.Router()
mongoose.connect(process.env.MONGODB_URL, () => console.log('This application has been connected to the Mogo DB!'))
router.get('/', async (req, res) => {
const ranking = await OpenseaScraper.rankings()
let NFTs = []
try {
ranking.forEach(async item => {
const slug = item.slug
const name = item.name
const logoUrl = item.logo
let floorPrice = 0
if (item.floorPrice == null) {
floorPrice = 0
} else {
floorPrice = item.floorPrice.amount
}
NFTs.push({name, slug, logoUrl, floorPrice})
NFT.findOne({ "slug": slug }, (err, value) => {
if (err) { console.error(err) }
if (!value) {
let newNFT = new NFT({slug, name, logoUrl, floorPrice})
newNFT.save() }
})
})
} catch(err) {
console.log(err)
res.status(400).end()
}
res.json(NFTs)
})
server.use(router)
server.listen(process.env.PORT || 5000, () => {
console.log(`Express is working on port ${process.env.PORT}`);
});
Here is the [crash] [1]: https://i.stack.imgur.com/8GvD4.png
2022-06-12T02:52:42.150870+00:00 heroku[web.1]: State changed from starting to up
2022-06-12T02:52:42.485695+00:00 app[web.1]: This application has been connected to the Mogo DB!
2022-06-12T02:53:19.480795+00:00 app[web.1]: /app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241
2022-06-12T02:53:19.480803+00:00 app[web.1]: reject(new Error([
2022-06-12T02:53:19.480803+00:00 app[web.1]: ^
2022-06-12T02:53:19.480803+00:00 app[web.1]:
2022-06-12T02:53:19.480804+00:00 app[web.1]: Error: Failed to launch the browser process!
2022-06-12T02:53:19.480818+00:00 app[web.1]: /app/node_modules/puppeteer/.local-chromium/linux-982053/chrome-linux/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
2022-06-12T02:53:19.480818+00:00 app[web.1]:
2022-06-12T02:53:19.480818+00:00 app[web.1]:
2022-06-12T02:53:19.480819+00:00 app[web.1]: TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
2022-06-12T02:53:19.480819+00:00 app[web.1]:
2022-06-12T02:53:19.480819+00:00 app[web.1]: at onClose (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241:20)
2022-06-12T02:53:19.480820+00:00 app[web.1]: at Interface.<anonymous> (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:231:68)
2022-06-12T02:53:19.480820+00:00 app[web.1]: at Interface.emit (node:events:539:35)
2022-06-12T02:53:19.480820+00:00 app[web.1]: at Interface.close (node:readline:586:8)
2022-06-12T02:53:19.480821+00:00 app[web.1]: at Socket.onend (node:readline:277:10)
2022-06-12T02:53:19.480822+00:00 app[web.1]: at Socket.emit (node:events:539:35)
2022-06-12T02:53:19.480822+00:00 app[web.1]: at endReadableNT (node:internal/streams/readable:1345:12)
2022-06-12T02:53:19.480822+00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:83:21)
2022-06-12T02:53:19.485994+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/" host=nft-backend-open-sea.herokuapp.com request_id=22e86188-d5ab-4e1a-a123-64ade9c345ab fwd="216.198.98.78" dyno=web.1 connect=0ms service=2510ms status=503 bytes=0 protocol=https
2022-06-12T02:53:19.594772+00:00 heroku[web.1]: Process exited with status 1
2022-06-12T02:53:19.717227+00:00 heroku[web.1]: State changed from up to crashed