[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-20。"],[],[],null,["# Update your web service\n\n### Region ID\n\nThe \u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e is an abbreviated code that Google assigns\nbased on the region you select when you create your app. The code does not\ncorrespond to a country or province, even though some region IDs may appear\nsimilar to commonly used country and province codes. For apps created after\nFebruary 2020, \u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e`.r` is included in\nApp Engine URLs. For existing apps created before this date, the\nregion ID is optional in the URL.\n\nLearn more\n[about region IDs](/appengine/docs/standard/nodejs/how-requests-are-routed#region-id). \nOK\n\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\n| **Note:** If you are deploying a new Node.js web service to Google Cloud, we recommend getting started with [Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-nodejs-service).\n\nThis section of the guide shows how to write, test, and deploy updates to the\nexample web service you deployed in the previous section,\n[Deploying Your Web Service](/appengine/docs/standard/nodejs/building-app/deploying-web-service).\n\nBefore you begin\n----------------\n\nIf you haven't already completed the previous sections in this \"Building an App\"\nguide, do the following:\n\n1. [Create a Google Cloud project with an App Engine app](/appengine/docs/standard/nodejs/building-app/creating-project).\n2. [Write a simple Node.js web service](/appengine/docs/standard/nodejs/building-app/writing-web-service).\n3. [Deploy the web service on App Engine](/appengine/docs/standard/nodejs/building-app/deploying-web-service).\n\nUpdate the example web service\n------------------------------\n\nThe following sections update the example web service with a form and a handler\nthat responds when a user submits the form.\n\n### Create a form for user input\n\nTo let a user submit data to your server, use an HTML form.\n\n1. In your `my-nodejs-service` folder, create a folder named `views`\n to store your HTML files.\n\n2. In the `views` folder, create a file named `form.html` and\n add the following code:\n\n \u003c!DOCTYPE html\u003e\n \u003chtml\u003e\n \u003chead\u003e\n \u003ctitle\u003eMy App Engine App\u003c/title\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003ch2\u003eCreate a new post\u003c/h2\u003e\n \u003cform method=\"POST\" action=\"/submit\"\u003e\n \u003cdiv\u003e\n \u003cinput type=\"text\" name=\"name\" placeholder=\"Name\"\u003e\n \u003c/div\u003e\n \u003cdiv\u003e\n \u003ctextarea name=\"message\" placeholder=\"Message\"\u003e\u003c/textarea\u003e\n \u003c/div\u003e\n \u003cdiv\u003e\n \u003cbutton type=\"submit\"\u003eSubmit\u003c/button\u003e\n \u003c/div\u003e\n \u003c/form\u003e\n \u003c/body\u003e\n \u003c/html\u003e\n\nThis simple form allows a user to enter a name and message to send to the\nserver. It sends the data via an HTTP `POST` request to `/submit`, as\nspecified by the `method` and `action` attributes on the `\u003cform\u003e` element.\n\nAt this point, you should have a file structure like the following: \n\n my-nodejs-service/\n views/\n form.html\n app.yaml\n package.json\n server.js\n\n### Display the form\n\n1. Add the following line to the top of your `server.js` file to import the\n `path` module:\n\n const path = require(`path`);\n\n2. Add the following Express handler to show the form when a user browses\n to `/submit`:\n\n app.get('/submit', (req, res) =\u003e {\n res.sendFile(path.join(__dirname, '/views/form.html'));\n });\n\n### Create a handler for submitted data\n\nWhen a user submits a message to the server, a `POST` request with the data is\nsent to `/submit`. To read the data from the body of the request, use Express\n`urlencoded` middleware and create a new request handler.\n\n1. Set your app to use Express `urlencoded` middleware:\n\n // This middleware is available in Express v4.16.0 onwards\n app.use(express.urlencoded({extended: true}));\n\n2. Add a `POST` handler to your `server.js` file to read the data:\n\n app.post('/submit', (req, res) =\u003e {\n console.log({\n name: req.body.name,\n message: req.body.message,\n });\n res.send('Thanks for your message!');\n });\n\nThis sample handler logs the user's name and message to the console, but you\ncould also perform operations on the data or store it in a database.\n\nTest the form locally\n---------------------\n\nTest your new form locally before deploying your changes.\n\n1. Start your Node.js server:\n\n npm start\n\n2. View your form at \u003chttp://localhost:8080/submit\u003e.\n\n Submit a message with the form. You should see your name and message\n appear in your terminal.\n\nDeploy your changes\n-------------------\n\nWhen you deploy an update, a new version of your default service is created\nand traffic is automatically routed to the latest version. To deploy:\n\n1. From your `my-nodejs-service` folder, run the following command:\n\n gcloud app deploy\n\n This is the same command you learned in\n [Deploy Your Web Service](/appengine/docs/standard/nodejs/building-app/deploying-web-service).\n2. Confirm that a new version is listed in the Google Cloud console:\n\n [View versions](https://console.cloud.google.com/appengine/versions)\n\n You should see two versions corresponding to the previous and current\n deployment.\n\nAfter deploying, your new form is accessible at\n\n`https://`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e`.`\u003cvar translate=\"no\"\u003e\u003ca href=\"#appengine-urls\" style=\"border-bottom: 1px dotted #999\" class=\"devsite-dialog-button\" data-modal-dialog-id=\"regional_url\" track-type=\"progressiveHelp\" track-name=\"modalHelp\" track-metadata-goal=\"regionalURL\"\u003eREGION_ID\u003c/a\u003e\u003c/var\u003e`.r.appspot.com``/submit`.\nUse it to submit a message or two!\n\nIf you do not need the previous version anymore, you can delete it from the\nversions page in the Google Cloud console.\n\nNext steps\n----------\n\nNow that your app has a form for users to submit data,\nlearn how to [view your application's logs in the Google Cloud console](/appengine/docs/standard/nodejs/building-app/viewing-service-logs)."]]