Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In diesem Leitfaden wird beschrieben, wie Sie einen Webdienst mit Node.js zur Ausführung in der App Engine-Standardumgebung schreiben. Weitere Informationen zur Node.js-Laufzeit und ihrer Funktionsweise finden Sie unter Node.js-Laufzeitumgebung.
Sie können Abhängigkeiten verwenden, nehmen Sie diese bei Bedarf in die Datei package.json auf. Weitere Informationen finden Sie unter Abhängigkeiten angeben.
App Engine startet Ihre Anwendung durch Ausführen von npm start.
Ihr Server muss den Port überwachen, der durch process.env.PORT angegeben wird, eine Umgebungsvariable, die von der App Engine-Laufzeit festgelegt wird.
Sie benötigen eine Datei app.yaml, um Ihren Dienst in App Engine bereitzustellen.
Server zur Überwachung von HTTP-Anfragen erstellen
Der HTTP-Server bildet den Kern Ihres Webdiensts. Im Beispielcode in diesem Leitfaden wird zur Verarbeitung von HTTP-Anfragen das Framework Express.js verwendet. Sie können jedoch ein Web-Framework Ihrer Wahl einsetzen.
Erstellen Sie einen neuen Ordner mit dem Namen my-nodejs-service für Ihren Node.js-Dienst.
Wechseln Sie zu dem Ordner in Ihrem Terminal und erstellen Sie eine Datei package.json durch Ausführen von npm init.
Fügen Sie Express als Abhängigkeit hinzu, indem Sie Folgendes ausführen:
npminstallexpress
Prüfen Sie, ob Express im Feld package.json der Datei dependencies angezeigt wird. Beispiel:
{..."dependencies":{"express":"^4.16.3"}...}
Fügen Sie der Datei package.json ein start-Skript hinzu:
"scripts":{"start":"node server.js"}
Erstellen Sie im selben Ordner eine Datei mit dem Namen server.js und fügen Sie folgenden Code hinzu:
constexpress=require('express');constapp=express();app.get('/',(req,res)=>{res.send('Hello from App Engine!');});// Listen to the App Engine-specified port, or 8080 otherwiseconstPORT=process.env.PORT||8080;app.listen(PORT,()=>{console.log(`Server listening on port ${PORT}...`);});
Dies ist ein sehr einfacher Webserver. Er antwortet auf alle GET-Anfragen an den Stammpfad ('/') mit dem Text "Hello from App Engine!" Beachten Sie die letzten vier Zeilen. Damit wird festgelegt, dass der Server den Port überwacht, der mit process.env.PORT angegeben wurde. Dies ist eine von der App Engine-Laufzeit festgelegte Umgebungsvariable. Wenn diese Anweisung fehlt, empfängt der Server keine Anfragen.
Ist process.env.PORT nicht angegeben, wird standardmäßig Port 8080 verwendet. Dies ist für das lokale Testen Ihrer Anwendung erforderlich, da process.env.PORT bei lokalen Ausführungen nicht festgelegt wird. Sie wird nur angegeben, wenn Ihre Anwendung in App Engine ausgeführt wird. Für das Testen können Sie einen beliebigen Port nutzen. In diesem Leitfaden wird 8080 verwendet.
Server lokal ausführen
So führen Sie den Server lokal aus:
Führen Sie dazu im Terminal npm start aus. Damit wird die Datei server.js ausgeführt.
Sie sollten eine Seite mit dem Text "Hello from App Engine!" sehen.
Erstellen der app.yaml-Datei
In der Datei app.yaml sind die Einstellungen für die Laufzeitumgebung des App Engine-Dienstes definiert. Ohne diese Datei lässt sich der Dienst nicht bereitstellen.
Erstellen Sie im Ordner my-nodejs-service eine Datei mit dem Namen app.yaml.
Fügen Sie den folgenden Inhalt hinzu:
runtime:nodejs20
Dies ist eine minimale Konfigurationsdatei, die für App Engine die Version der Node.js-Laufzeit angibt. In der Datei app.yaml können auch Netzwerk- und Skalierungseinstellungen festgelegt werden. Weitere Informationen finden Sie in der app.yaml-Referenz.
Sie haben jetzt einen einfachen Node.js-Webserver erstellt, der den richtigen Port überwacht, und Sie haben die Laufzeit in der Datei app.yaml angegeben. Als Nächstes können Sie den Dienst in App Engine bereitstellen.
Überzeugen Sie sich selbst
Wenn Sie mit Google Cloud noch nicht vertraut sind, erstellen Sie einfach ein Konto, um die Leistungsfähigkeit von App Engine in der Praxis sehen und bewerten zu können. Neukunden erhalten außerdem ein Guthaben von 300 $, um Arbeitslasten auszuführen, zu testen und bereitzustellen.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[[["\u003cp\u003eThis guide details how to create a Node.js web service for the App Engine standard environment, using the \u003ccode\u003epackage.json\u003c/code\u003e file for dependencies and \u003ccode\u003eapp.yaml\u003c/code\u003e for deployment configurations.\u003c/p\u003e\n"],["\u003cp\u003eApp Engine applications start via the \u003ccode\u003enpm start\u003c/code\u003e command, which you must configure in the \u003ccode\u003epackage.json\u003c/code\u003e file, typically to run your main server file.\u003c/p\u003e\n"],["\u003cp\u003eYour Node.js server must listen to the port specified by the \u003ccode\u003eprocess.env.PORT\u003c/code\u003e environment variable provided by App Engine to receive external requests.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eapp.yaml\u003c/code\u003e file is essential for deploying your service to App Engine, as it defines the runtime environment, such as the Node.js version.\u003c/p\u003e\n"],["\u003cp\u003eTo run and test the application locally, use \u003ccode\u003enpm start\u003c/code\u003e and visit \u003ccode\u003ehttp://localhost:8080\u003c/code\u003e in your browser, ensuring port 8080 is used as a fallback when \u003ccode\u003eprocess.env.PORT\u003c/code\u003e is not set.\u003c/p\u003e\n"]]],[],null,["# Write your web service with Node.js\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 guide shows how to write a Node.js web service to\nrun in the App Engine standard environment. To learn more about\nthe Node.js runtime and how it works, see\n[Node.js Runtime Environment](/appengine/docs/standard/nodejs/runtime).\n\nBefore you begin\n----------------\n\n- [Install Node.js LTS](https://nodejs.org/en/download/).\n\nKey points\n----------\n\n- You can use dependencies by listing them in your [`package.json`](https://docs.npmjs.com/files/package.json) file. See [Specifying Dependencies](/appengine/docs/standard/nodejs/specifying-dependencies) for more information.\n- App Engine starts your application by running `npm start`.\n- Your server must listen to the port specified by the `process.env.PORT`, an [environment variable](/appengine/docs/standard/nodejs/runtime#environment_variables) set by the App Engine runtime.\n- You need an [`app.yaml`](/appengine/docs/standard/nodejs/configuring-your-app-with-app-yaml) file to deploy your service to App Engine.\n\nCreate a server to listen for HTTP requests\n-------------------------------------------\n\nThe core of your web service is the HTTP server. The sample code in this guide\nuses the [Express.js](https://expressjs.com/)\nframework to handle HTTP requests, but you are free to use a web framework of\nyour choice.\n\n1. Create a new folder called `my-nodejs-service` for your Node.js\n service.\n\n2. Navigate to the folder in your terminal, and create a `package.json` file\n by running `npm init`.\n\n3. Add Express as a dependency by running:\n\n npm install express\n\n Confirm that Express appears in your `package.json` file's `dependencies`\n field. Here's an example: \n\n {\n ...\n \"dependencies\": {\n \"express\": \"^4.16.3\"\n }\n ...\n }\n\n4. Add a `start` script to your `package.json` file:\n\n \"scripts\": {\n \"start\": \"node server.js\"\n }\n\n5. Create a file called `server.js` in the same folder and add the following\n code:\n\n const express = require('express');\n const app = express();\n\n app.get('/', (req, res) =\u003e {\n res.send('Hello from App Engine!');\n });\n\n // Listen to the App Engine-specified port, or 8080 otherwise\n const PORT = process.env.PORT || 8080;\n app.listen(PORT, () =\u003e {\n console.log(`Server listening on port ${PORT}...`);\n });\n\nThis is a very basic web server - it responds to all `GET` requests to the root\npath (`'/'`) with the text \"Hello from App Engine!\" Note the last four lines,\nwhere the server is set to listen to the port specified by `process.env.PORT`,\nan [environment variable](/appengine/docs/standard/nodejs/runtime#environment_variables)\nset by the App Engine runtime. If your server isn't set to listen to\nthis port, it will not receive requests.\n\nNotice that if `process.env.PORT` is not set, port `8080` is used as\na default. This is necessary for testing your app locally, because\n`process.env.PORT` doesn't get set during local runs - it is only set when\nyour app is running on App Engine. You can use whichever port\nyou prefer for testing, but this guide uses `8080`.\n\nRun the server locally\n----------------------\n\nTo run the server locally:\n\n1. Run `npm start` in your terminal. This will run your `server.js` file.\n\n2. Point your web browser to \u003chttp://localhost:8080\u003e.\n\nYou should see a page with the text \"Hello from App Engine!\"\n\nCreate the `app.yaml` file\n--------------------------\n\nAn `app.yaml` file specifies settings for your App Engine service's\nruntime environment. Your service will not deploy without this file.\n\n1. In your `my-nodejs-service` folder, create a file called\n `app.yaml`.\n\n2. Add the following contents:\n\n runtime: nodejs20\n\n This is a minimal configuration file, indicating to App Engine the\n version of the Node.js runtime. The `app.yaml` file can also specify network\n settings, scaling settings, and more. For more information, see the\n [`app.yaml` reference](/appengine/docs/standard/reference/app-yaml).\n\nAt this point, you should have a file structure like the following: \n\n my-nodejs-service/\n app.yaml\n package.json\n server.js\n\nNext steps\n----------\n\nNow that you've created a simple Node.js web server that listens to the correct\nport and you've specified the runtime in an `app.yaml` file, you're ready to\n[deploy your service on App Engine](/appengine/docs/standard/nodejs/building-app/deploying-web-service).\n\nTry it for yourself\n-------------------\n\n\nIf you're new to Google Cloud, create an account to evaluate how\nApp Engine performs in real-world\nscenarios. New customers also get $300 in free credits to run, test, and\ndeploy workloads.\n[Try App Engine free](https://console.cloud.google.com/freetrial)"]]