[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-09 (世界標準時間)。"],[],[],null,["\u003cbr /\u003e\n\nYou can trigger a function through an HTTP request by using\n`functions.https`. This allows you to invoke a synchronous function through\nthe following supported HTTP methods: `GET`, `POST`, `PUT`, `DELETE`, and\n`OPTIONS`.\n\nExamples in this page are based on a\n[sample function](https://github.com/firebase/functions-samples/tree/main/Node-1st-gen/quickstarts/https-time-server)\nthat triggers when you send\nan HTTP `GET` request to the functions endpoint. The sample function\nretrieves the current server time, formats the time as specified in a URL query\nparameter, and sends the result in the HTTP response.\n\nTrigger a function with an HTTP request\n\nUse [`functions.https`](/docs/reference/functions/firebase-functions.https)\nto create a function that handles HTTP\nevents. The event handler for an HTTP function listens for the\n[`onRequest()`](/docs/reference/functions/firebase-functions.https#httpsonrequest) event,\nwhich supports routers and apps managed by\nthe [Express](https://expressjs.com/) web framework.\n\nUsing Express request and response objects\n\nUsed as arguments for `onRequest()`,\nthe [Request](http://expressjs.com/en/4x/api.html#req) object gives\nyou access to the properties of the HTTP request sent by the client, and the\n[Response](http://expressjs.com/en/4x/api.html#res) object gives you a way to\nsend a response back to the client.\n\n\u003cbr /\u003e\n\n```gdscript\nexports.date = functions.https.onRequest((req, res) =\u003e {\n // ...\n});\n```\n\n\u003cbr /\u003e\n\n| **Important:** Make sure that all HTTP functions terminate properly. By terminating functions correctly, you can avoid excessive charges from functions that run for too long. Terminate **HTTP functions** with `res.redirect()`, `res.send()`, or `res.end()`.\n\nUsing existing Express apps\n\nUsing [App](http://expressjs.com/en/4x/api.html#req.app) as the argument for\n`onRequest()`, you can\npass a full Express app to an HTTP function. Boilerplate code can be moved\nto middleware as shown: \n\n const express = require('express');\n const cors = require('cors');\n\n const app = express();\n\n // Automatically allow cross-origin requests\n app.use(cors({ origin: true }));\n\n // Add middleware to authenticate requests\n app.use(myMiddleware);\n\n // build multiple CRUD interfaces:\n app.get('/:id', (req, res) =\u003e res.send(Widgets.getById(req.params.id)));\n app.post('/', (req, res) =\u003e res.send(Widgets.create()));\n app.put('/:id', (req, res) =\u003e res.send(Widgets.update(req.params.id, req.body)));\n app.delete('/:id', (req, res) =\u003e res.send(Widgets.delete(req.params.id)));\n app.get('/', (req, res) =\u003e res.send(Widgets.list()));\n\n // Expose Express API as a single Cloud Function:\n exports.widgets = functions.https.onRequest(app);\n\nInvoke an HTTP function\n\nAfter you deploy an HTTP function, you can invoke it through its own unique\nURL. The URL includes the following, in order:\n\n- The region (or regions) to which you deployed your function. Some production functions may need to explicitly set the [location](/docs/functions/locations) to minimize network latency.\n- Your Firebase project ID\n- `cloudfunctions.net`\n- The name of your function\n\nFor example, the URL to invoke `date()` looks like this: \n\n https://us-central1-\u003cproject-id\u003e.cloudfunctions.net/date\n\nIf you encounter permissions errors when deploying functions, make sure that\nthe appropriate [IAM roles](/docs/projects/iam/permissions#functions) are\nassigned to the user running the deployment commands.\n\nWith Express app routing, the function name is added as a prefix to the URL paths\nin the app you define. For example, the URL to invoke the getter in the Express\napp example above looks like this: \n\n https://us-central1-\u003cproject-id\u003e.cloudfunctions.net/widgets/\u003cid\u003e\n\nIf you invoke HTTP functions behind a firewall or IP filter,\nyou can [look up](https://cloud.google.com/compute/docs/faq#find_ip_range)\nthe IP addresses that Google uses to serve HTTP functions.\n\nUse middleware modules with Cloud Functions\n\nIf you need to inject middleware dependencies for things like cookie support or\nCORS, call these within the function. For example, to enable CORS support, add\nthe following block:\n\n\u003cbr /\u003e\n\n```mysql\n// Enable CORS using the `cors` express middleware.\ncors(req, res, () =\u003e {\n // ...\n});\n```\n\n\u003cbr /\u003e\n\nRead values from the request The following table lists some common scenarios:\n\n\u003cbr /\u003e\n\n| Content Type | Request Body | Behavior |\n|-------------------------------------|---------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|\n| `application/json` | `'{\"name\":\"John\"}'` | `request.body.name` equals 'John' |\n| `application/octet-stream` | 'my text' | `request.body` equals '6d792074657874' (the raw bytes of the request; see the [Node.js Buffer documentation](https://nodejs.org/api/buffer.html)) |\n| `text/plain` | 'my text' | `request.body` equals 'my text' |\n| `application/x-www-form-urlencoded` | 'name=John' | `request.body.name` equals 'John' |\n\nThis parsing is done by the following body parsers:\n\n- [JSON body parser](https://www.npmjs.com/package/body-parser#bodyparserjsonoptions)\n- [Raw body parser](https://www.npmjs.com/package/body-parser#bodyparserrawoptions)\n- [Text body parser](https://www.npmjs.com/package/body-parser#bodyparsertextoptions)\n- [URL-encoded form body parser](https://www.npmjs.com/package/body-parser#bodyparserurlencodedoptions)\n\nSuppose your function is called with the following request: \n\n```bash\ncurl -X POST -H \"Content-Type:application/json\" -H \"X-MyHeader: 123\" YOUR_HTTP_TRIGGER_ENDPOINT?foo=baz -d '{\"text\":\"something\"}'\n```\n\nthen the sent data would be materialized under:\n\n| Property/Method | Value |\n|-------------------------|-----------------------------------------|\n| `req.method` | \"POST\" |\n| `req.get('x-myheader')` | \"123\" |\n| `req.query.foo` | \"baz\" |\n| `req.body.text` | \"something\" |\n| `req.rawBody` | The raw (unparsed) bytes of the request |\n\nIn the `date()` function example, the function tests both the URL parameter and\nthe body for a `format` value to set the date/time format to use:\n\n\u003cbr /\u003e\n\n```text\nlet format = req.query.format;\nformat = req.body.format;https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node-1st-gen/quickstarts/https-time-server/functions/index.js#L69-L69\n```\n\n\u003cbr /\u003e\n\nTerminate HTTP Functions\n\nAlways end an HTTP function with `send()`, `redirect()`,\nor `end()`. Otherwise, your function might continue to run and be\nforcibly terminated by the system. See also\n[Sync, Async and Promises](/docs/functions/terminate-functions).\n\nAfter retrieving and formatting the server time using the Node.js\n[`moment`](https://www.npmjs.com/package/moment) module, the `date()` function\nconcludes by sending the result in the HTTP response:\n\n\u003cbr /\u003e\n\n```gdscript\nconst formattedDate = moment().format(`${format}`);\nfunctions.logger.log('Sending Formatted date:', formattedDate);\nres.status(200).send(formattedDate);https://github.com/firebase/functions-samples/blob/c4fde45b65fab584715e786ce3264a6932d996ec/Node-1st-gen/quickstarts/https-time-server/functions/index.js#L73-L75\n```\n\n\u003cbr /\u003e\n\nConnecting HTTP Functions to Firebase Hosting\n\nYou can connect an HTTP function to Firebase Hosting. Requests on your\nFirebase Hosting site can be proxied to specific HTTP functions. This also\nallows you to use your own custom domain with an HTTP function. Learn more about\n[connecting Cloud Functions to Firebase Hosting](/docs/hosting/functions).\n| **Note:** If you're using a web framework like Angular Universal or Next.js to develop dynamic web apps, try out the public preview of the [framework-aware Firebase CLI](/docs/hosting/frameworks-overview). This early preview supports both static site generation and server-side rendering."]]