Skip to content

Commit

Permalink
Update: Use 0.0.0.0 always
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Oct 8, 2020
1 parent bb1d1f6 commit 172e16d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ To start the server, run:
$ npm start
```

Locally, this will run the server on http://127.0.0.1:8080.
Locally, this will run the server on http://localhost:8080.

## Notes

1. The server reads from the `PORT` environment variable to determine which port to listen on. This is important because Google Cloud Run defines this variable and it's the only port that will be open.
1. Google Cloud Run also requires that the server listen on 0.0.0.0 because 127.0.0.1 is a loopback address with no external access. I make this change by looking for the `K_SERVICE` environment variable that Cloud Run defines and then switching the address to 0.0.0.0.
1. Google Cloud Run also requires that the server listen on 0.0.0.0 because 127.0.0.1 is a loopback address with no external access.
1. You no longer need to define a `Dockerfile` to run Node.js projects on Google Cloud Run. You can instead use the built-in [Node.js Buildpack](https://github.com/GoogleCloudPlatform/buildpacks). All you need to do is define `npm start`.

## License
Expand Down
9 changes: 1 addition & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ const fastify = require('fastify')({ logger: true })
//-----------------------------------------------------------------------------

const { PORT=8080, K_SERVICE } = process.env;

/*
* Cloud Run exposes a K_SERVICE environment variable. So, I'm using that to
* determine when the server is running in Cloud Run vs. running in a dev
* environment. Cloud Run needs to listen on 0.0.0.0 but in a dev environment
* it's a lot to easier to work with 127.0.0.1.
*/
const HOST = K_SERVICE ? "0.0.0.0" : "127.0.0.1";
const HOST = "0.0.0.0";

//-----------------------------------------------------------------------------
// Server
Expand Down

0 comments on commit 172e16d

Please sign in to comment.