I'm trying to deploy a 2nd generation cloud function as a zip file. The code is the following:
// index.js
const functions = require('@google-cloud/functions-framework');
functions.http('mp4Transcoder', (req, res) => {});
// dev-server.js
#!/usr/bin/env node
const express = require('express')
const app = express()
const router = express.Router()
const func = require('./index')
const bodyParser = require('body-parser')
app.use(bodyParser.raw({ type: 'application/octet-stream' }))
router.post('/transcoder', func.transcode)
router.options('/transcoder', func.transcode)
app.use('/', router)
// start the server
const port = 3030
const http = require('http')
app.set('port', port)
const server = http.createServer(app)
server.listen(port)
// package.json
{
"name": "transcoder-cloud-function",
"dependencies": {
"@google-cloud/functions-framework": "^3.0.0"
}
}
If I copy and paste this code and deploy the cloud function, it works. However, if I zip the three files and try to create uploading the zip file, the deployment fails with "function.js does not exist".
Any ideas of what I could try?
CodePudding user response:
If you are trying to deploy an Express app to Cloud Function then you should be passing the Express app instance to the Cloud Function and not use the function as a router. Try refactoring the code as shown below:
// index.js
const functions = require('@google-cloud/functions-framework');
const { app } = require('./dev-server.js');
functions.http('mp4transcoder', app);
// dev-server.js
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
app.use(bodyParser.raw({ type: 'application/octet-stream' }))
app.use(bodyParser.json())
app.get('/', (req, res) => {
res.send('Hello World!')
})
// app.use('/', router)
exports.app = app
// package.json
{
"name": "transcoder-cloud-function",
"dependencies": {
"@google-cloud/functions-framework": "^3.0.0",
"body-parser": "^1.20.1"
}
}
Then try redeploying the function either by uploading the ZIP file or directly using gcloud
CLI:
gcloud functions deploy mp4transcoder --gen2 --trigger-http --runtime=nodejs16
Also checkout Cloud Function for Firebase as its a bit easier to deploy that way.
CodePudding user response:
Ok, the answer was simpler than I expected: the zip file had the wrong file structure. Google Cloud is expecting the folder structure zip > your files, not zip > folder > your files, as was the case.