Skip to content

Instantly share code, notes, and snippets.

@tatsuyasusukida
Last active September 6, 2022 18:51
Show Gist options
  • Save tatsuyasusukida/d73440930fbc2202664eb12456136934 to your computer and use it in GitHub Desktop.
Save tatsuyasusukida/d73440930fbc2202664eb12456136934 to your computer and use it in GitHub Desktop.
Node.js Cloud Run Example
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)
}
}
{
"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