Search code examples
node.jspostgresqlapiexpressheroku

Heroku deployment with node.js and postgresql api not working


I have been recently been working on a node.js api with a postgresql database that stores books. When you go to the route /books it gives me the heroku application error page (here is the api https://node-api-with-books.herokuapp.com/books) and I followed this tutorial to make it https://www.taniarascia.com/node-express-postgresql-heroku/. It is driving me crazy and here is the error it gives when I do:

heroku logs --tail
2021-03-15T04:04:21.094893+00:00 app[web.1]: /app/index.js:12
2021-03-15T04:04:21.094902+00:00 app[web.1]: throw err;
2021-03-15T04:04:21.094903+00:00 app[web.1]: ^
2021-03-15T04:04:21.094904+00:00 app[web.1]: 
2021-03-15T04:04:21.094904+00:00 app[web.1]: Error: self signed certificate
2021-03-15T04:04:21.094905+00:00 app[web.1]: at TLSSocket.onConnectSecure (node:_tls_wrap:1498:34)
2021-03-15T04:04:21.094906+00:00 app[web.1]: at TLSSocket.emit (node:events:327:20)
2021-03-15T04:04:21.094906+00:00 app[web.1]: at TLSSocket._finishInit (node:_tls_wrap:933:8)
2021-03-15T04:04:21.094907+00:00 app[web.1]: at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:707:12) {
2021-03-15T04:04:21.094907+00:00 app[web.1]: code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
2021-03-15T04:04:21.094907+00:00 app[web.1]: }
2021-03-15T04:04:21.099759+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/books" host=node-api-with-books.herokuapp.com request_id=5ec4a052-1aed-4401-916d-1699f1fe9d3d fwd="66.7.125.54" dyno=web.1 connect=0ms service=50ms status=503 bytes=0 protocol=https
2021-03-15T04:04:21.100313+00:00 app[web.1]: npm notice
2021-03-15T04:04:21.100365+00:00 app[web.1]: npm notice New minor version of npm available! 7.0.8 -> 7.6.3
2021-03-15T04:04:21.100444+00:00 app[web.1]: npm notice Changelog: <https://github.com/npm/cli/releases/tag/v7.6.3>
2021-03-15T04:04:21.100541+00:00 app[web.1]: npm notice Run `npm install -g [email protected]` to update!
2021-03-15T04:04:21.100619+00:00 app[web.1]: npm notice
2021-03-15T04:04:21.104741+00:00 app[web.1]: npm ERR! code 1
2021-03-15T04:04:21.104926+00:00 app[web.1]: npm ERR! path /app
2021-03-15T04:04:21.108208+00:00 app[web.1]: npm ERR! command failed
2021-03-15T04:04:21.108390+00:00 app[web.1]: npm ERR! command sh -c node index.js
2021-03-15T04:04:21.353877+00:00 app[web.1]: 
2021-03-15T04:04:21.354099+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-03-15T04:04:21.354257+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-03-15T04_04_21_109Z-debug.log
2021-03-15T04:04:21.413929+00:00 heroku[web.1]: Process exited with status 1
2021-03-15T04:04:21.490613+00:00 heroku[web.1]: State changed from up to crashed
2021-03-15T04:04:21.495328+00:00 heroku[web.1]: State changed from crashed to starting
2021-03-15T04:04:24.629509+00:00 heroku[web.1]: Starting process with command `npm start`
2021-03-15T04:04:27.698683+00:00 app[web.1]: 
2021-03-15T04:04:27.698698+00:00 app[web.1]: > [email protected] start
2021-03-15T04:04:27.698698+00:00 app[web.1]: > node index.js
2021-03-15T04:04:27.698698+00:00 app[web.1]: 
2021-03-15T04:04:28.035756+00:00 app[web.1]: Server listening
2021-03-15T04:04:28.553149+00:00 heroku[web.1]: State changed from starting to up
2021-03-15T04:04:29.332623+00:00 app[web.1]: /app/index.js:12
2021-03-15T04:04:29.332639+00:00 app[web.1]: throw err;
2021-03-15T04:04:29.332640+00:00 app[web.1]: ^
2021-03-15T04:04:29.332641+00:00 app[web.1]: 
2021-03-15T04:04:29.332641+00:00 app[web.1]: Error: self signed certificate
2021-03-15T04:04:29.332642+00:00 app[web.1]: at TLSSocket.onConnectSecure (node:_tls_wrap:1498:34)
2021-03-15T04:04:29.332643+00:00 app[web.1]: at TLSSocket.emit (node:events:327:20)
2021-03-15T04:04:29.332643+00:00 app[web.1]: at TLSSocket._finishInit (node:_tls_wrap:933:8)
2021-03-15T04:04:29.332643+00:00 app[web.1]: at TLSWrap.ssl.onhandshakedone (node:_tls_wrap:707:12) {
2021-03-15T04:04:29.332644+00:00 app[web.1]: code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
2021-03-15T04:04:29.332644+00:00 app[web.1]: }
2021-03-15T04:04:29.340112+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/books" host=node-api-with-books.herokuapp.com request_id=8af9f82c-42c9-4399-9de3-ed80f7dee15a fwd="66.7.125.54" dyno=web.1 connect=1ms service=74ms status=503 bytes=0 protocol=https
2021-03-15T04:04:29.343368+00:00 app[web.1]: npm notice
2021-03-15T04:04:29.343566+00:00 app[web.1]: npm notice New minor version of npm available! 7.0.8 -> 7.6.3
2021-03-15T04:04:29.343688+00:00 app[web.1]: npm notice Changelog: <https://github.com/npm/cli/releases/tag/v7.6.3>
2021-03-15T04:04:29.343859+00:00 app[web.1]: npm notice Run `npm install -g [email protected]` to update!
2021-03-15T04:04:29.344058+00:00 app[web.1]: npm notice
2021-03-15T04:04:29.348863+00:00 app[web.1]: npm ERR! code 1
2021-03-15T04:04:29.349087+00:00 app[web.1]: npm ERR! path /app
2021-03-15T04:04:29.352585+00:00 app[web.1]: npm ERR! command failed
2021-03-15T04:04:29.352766+00:00 app[web.1]: npm ERR! command sh -c node index.js
2021-03-15T04:04:29.364318+00:00 app[web.1]: 
2021-03-15T04:04:29.364464+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-03-15T04:04:29.364515+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-03-15T04_04_29_354Z-debug.log
2021-03-15T04:04:29.443525+00:00 heroku[web.1]: Process exited with status 1
2021-03-15T04:04:29.510747+00:00 heroku[web.1]: State changed from up to crashed

I can add you on heroku if you have a idea Here is my code on github https://github.com/PugmanD/gticlicker-api-and-book-api/tree/main

Any help is appreciated


Solution

  • After trying with the uploaded repo. I've confirmed the error lies at the following place

    config.js

    const pool = new Pool({
        connectionString: isProduction ? process.env.DATABASE_URL : connectionString,
        ssl: isProduction, // The problem is this setting
    });
    

    Update your config.js to following

    const pool = new Pool({
      connectionString: isProduction ? process.env.DATABASE_URL : connectionString,
      ssl: {
        rejectUnauthorized: false,
      },
    });
    

    node-postgres did show how to setup the SSL in their documentation https://node-postgres.com/features/ssl

    The problem why u faced this is most likely that the node-postgres couldn't find your SSL cert, hence the error.

    This is the heroku link that I've make it working, I will remove the project in a few days. Feel free to try it. https://test-postgresl.herokuapp.com/books