Last active
September 6, 2022 18:51
-
-
Save tatsuyasusukida/d73440930fbc2202664eb12456136934 to your computer and use it in GitHub Desktop.
Node.js Cloud Run Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http') | |
if (require.main === module) { | |
main() | |
} | |
async function main () { | |
try { | |
const server = http.createServer() // <1> | |
server.on('listening', () => { // <2> | |
console.info(`Listening on ${process.env.PORT}`) | |
}) | |
server.on('request', (req, res) => { // <3> | |
const content = JSON.stringify({ok: true}) | |
res.writeHead(200, { | |
'Content-Type': 'application/json; charset=utf-8', | |
'Content-Length': content.length, | |
}) | |
res.write(content) | |
res.end() | |
}) | |
server.on('error', err => { // <4> | |
console.error(err) | |
}) | |
server.listen(process.env.PORT) // <5> | |
} catch (err) { | |
console.error(err) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"scripts": { | |
"start": "node main.js", | |
"build": "pack build column-cloudrun-nodejs --builder gcr.io/buildpacks/builder", | |
"image": "echo asia-northeast1-docker.pkg.dev/`gcloud config get-value project`/cloud-run/column-cloudrun-nodejs", | |
"tag": "docker image tag column-cloudrun-nodejs `npm run -s image`", | |
"push": "docker image push `npm run -s image`", | |
"deploy": "gcloud run deploy column-cloudrun-nodejs --image `npm run -s image` --region asia-northeast1 --platform managed --allow-unauthenticated", | |
"staging": "npm run build && npm run tag && npm run push && npm run deploy", | |
"delete": "gcloud run services delete column-cloudrun-nodejs --region asia-northeast1 --platform managed" | |
}, | |
"engines": { | |
"node": "^16.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment