יצירת אפליקציית Node.js בסביבה גמישה ב-App Engine

מזהה אזור

הערך REGION_ID הוא קוד מקוצר ש-Google מקצה על סמך האזור שאתם בוחרים כשאתם יוצרים את האפליקציה. הקוד לא תואם למדינה או למחוז, למרות שחלק ממזהי האזורים עשויים להיראות דומים לקודים נפוצים של מדינות ומחוזות. באפליקציות שנוצרו אחרי פברואר 2020, REGION_ID.r נכלל בכתובות ה-URL של App Engine. באפליקציות קיימות שנוצרו לפני התאריך הזה, מזהה האזור הוא אופציונלי בכתובת ה-URL.

מידע נוסף על מזהי אזורים

במדריך למתחילים הזה נסביר איך ליצור ולפרוס אפליקציה שמציגה הודעה קצרה. אתם יכולים להשתמש באפליקציה לדוגמה שבמדריך למתחילים הזה בכל גרסה נתמכת של Node.js, על ידי ציון גרסת זמן הריצה ומערכת ההפעלה בקובץ app.yaml.

לפני שמתחילים

  1. נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloudליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Build API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Cloud Build API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init

התפקידים הנדרשים

כדי לקבל את ההרשאות שדרושות להשלמת המדריך למתחילים, אתם צריכים לבקש מהאדמין לתת לכם את התפקידים הבאים ב-IAM:

להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.

דרישות מוקדמות נוספות

  1. מאתחלים את אפליקציית App Engine עם הפרויקט ובוחרים את האזור שלה:

    gcloud app create --project=[YOUR_PROJECT_ID]
    

    כשמופיעה בקשה, בוחרים את האזור שבו רוצים למקם את אפליקציית App Engine.

  2. מתקינים את הכלים הבאים במחשב המקומי:

המיקומים של App Engine

‫App Engine הוא אזורי, כלומר התשתית שמריצה את האפליקציות שלכם נמצאת באזור ספציפי, ו-Google מנהלת אותה כך שהיא זמינה באופן יתיר בכל התחומים באותו אזור.

הגורמים העיקריים לבחירת האזור שבו האפליקציות שלכם יפעלו הם הדרישות שלכם לגבי זמן אחזור, זמינות או עמידות. בדרך כלל אפשר לבחור את האזור הכי קרוב למשתמשים של האפליקציה, אבל כדאי להביא בחשבון את המיקומים שבהם App Engine זמין ואת המיקומים שלGoogle Cloud המוצרים והשירותים האחרים שבהם האפליקציה משתמשת. שימוש בשירותים בכמה מיקומים יכול להשפיע על זמן האחזור של האפליקציה ועל התמחור שלה.

אי אפשר לשנות את האזור של האפליקציה אחרי שמגדירים אותו.

אם כבר יצרתם אפליקציה ב-App Engine, אתם יכולים לראות את האזור שלה באחת מהדרכים הבאות:

במדריך למתחילים הזה נדגים אפליקציית Node.js פשוטה שכתובה באמצעות מסגרת Express.js.

הורדת האפליקציה Hello World

יצרנו אפליקציית Hello World ל-App Engine כדי שתוכלו להבין במהירות איך לפרוס אפליקציה ב- Google Cloud.

  1. משכפלים את המאגר של האפליקציה לדוגמה Hello World למכונה המקומית.

    git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
    

    אפשר גם להוריד את הקובץ לדוגמה כקובץ ZIP ולחלץ אותו.

  2. עוברים לספרייה שמכילה את הקוד לדוגמה.

       cd nodejs-docs-samples/appengine/hello-world/flexible/
    

הרצת Hello World במחשב המקומי

  1. מתקינים את יחסי התלות של הפרויקט.

    npm install
    

    או אם אתם משתמשים ב-Yarn.

    yarn install
    
  2. מריצים את הסקריפט start.

    npm start
    

    או אם אתם משתמשים ב-Yarn.

    yarn start
    
  3. פותחים את האפליקציה בדפדפן האינטרנט.

    • אם אתם משתמשים ב-Cloud Shell, בסרגל הכלים לוחצים על Web Preview ובוחרים באפשרות Preview on port 8080.

    • אם אתם לא משתמשים ב-Cloud Shell, הזינו את הכתובת הבאה:

    http://localhost:8080

ההודעה Hello World מהאפליקציה לדוגמה מוצגת בדף. בחלון הטרמינל, מקישים על Ctrl+C כדי לצאת משרת האינטרנט.

פריסה והפעלה של Hello World ב-App Engine

כדי לפרוס את האפליקציה בסביבה הגמישה של App Engine:

  1. מריצים את הפקודה הבאה מהספרייה hello-world/flexible/ כדי לפרוס את האפליקציה Hello World:

    gcloud app deploy

    מידע על הדגלים האופציונליים.

    דגלים נפוצים של פקודות gcloud

    • מוסיפים את הדגל --version כדי לציין מזהה ייחודי לגרסה הזו של האפליקציה. אם לא תציינו מזהה, המערכת תיצור אחד בשבילכם. לדוגמה: --version [YOUR_VERSION_ID]
    • מוסיפים את הדגל --project כדי לציין מזהה פרויקט ב- Google Cloud שונה מזה שהגדרתם כברירת מחדל בכלי gcloud. לדוגמה: --project [YOUR_PROJECT_ID]

    דוגמה:

    gcloud app deploy --version pre-prod-5 --project my-sample-app

    במאמר בדיקה ופריסה של האפליקציה מוסבר איך לפרוס את האפליקציה משורת הפקודה. רשימה של כל הדגלים של הפקודות מופיעה במאמרי העזרה של gcloud app deploy.

  2. מפעילים את הדפדפן כדי להציג את האפליקציה בכתובת https://PROJECT_ID.REGION_ID.r.appspot.com

    ‫
    gcloud app browse
    , כאשר PROJECT_ID מייצג את מזהה הפרויקט ב- Google Cloud.

הפעם, הדף שבו מוצגת ההודעה Hello World מועבר על ידי שרת אינטרנט שפועל במכונה של App Engine.

כל הכבוד! פרסתם את האפליקציה הראשונה שלכם ב-App Engine בסביבה הגמישה ב-App Engine!

אם נתקלתם בשגיאות בפריסת האפליקציה, כדאי לעיין בטיפים לפתרון בעיות.

בקטעים הבאים מוסבר איך לנקות את המשאבים, ויש קישורים לפעולות נוספות שאפשר לבצע.

הסרת המשאבים

כדי להימנע מצבירת חיובים, אתם יכולים למחוק את הפרויקט ב- Google Cloud כדי להפסיק את החיוב על כל המשאבים שבהם השתמשתם בפרויקט.

  1. במסוף Google Cloud , נכנסים לדף Manage resources.

    כניסה לדף Manage resources

  2. ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
  3. כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.

המאמרים הבאים

לומדים את כל הפלטפורמה

עכשיו, אחרי שהתנסיתם בפיתוח ובפריסה של אפליקציות ב-App Engine, אתם יכולים להמשיך לחקור את Google Cloud. כבר התקנתם את Google Cloud CLI, שכולל את הכלים לאינטראקציה עם מוצרים כמו Cloud SQL, ‏ Cloud Storage,‏ Firestore ועוד.

מידע על הסביבה הגמישה ב-App Engine

הנה כמה נושאים שיעזרו לכם להמשיך ללמוד על App Engine:

סקר קוד של Hello World

‫Hello World היא האפליקציה הכי פשוטה של App Engine, כי היא מכילה רק שירות אחד, יש לה רק גרסה אחת וכל הקוד נמצא בתיקיית השורש של האפליקציה. בקטע הזה מפורטים כל קובצי האפליקציה.

app.js

הקובץ app.js מכיל את קוד ה-JavaScript להפעלת שרת ולהגשת תשובות לבקשות.

הקוד הבא מגיב לבקשות מלקוחות אינטרנט על ידי הצגת ההודעה "Hello World", באמצעות שרת שפועל ביציאה 8080.

const express = require('express');

const app = express();

app.get('/', (req, res) => {
  res.status(200).send('Hello, world!').end();
});

// Start the server
const PORT = parseInt(process.env.PORT) || 8080;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});

app.yaml

הקובץ app.yaml מתאר את ההגדרות הבאות של האפליקציה:

  • מגדיר את env: flex, כדי לציין שהאפליקציה משתמשת בסביבה גמישה ב-App Engine.
  • מציינת את סביבת זמן הריצה שבה האפליקציה משתמשת.

    runtime: nodejs
    env: flex
    runtime_config:
      operating_system: ubuntu24
    # This sample incurs costs to run on the App Engine flexible environment.
    # The settings below are to reduce costs during testing and are not appropriate
    # for production use. For more information, see:
    # https://cloud.google.com/appengine/docs/flexible/nodejs/configuring-your-app-with-app-yaml
    manual_scaling:
      instances: 1
    resources:
      cpu: 1
      memory_gb: 0.5
      disk_size_gb: 10
    

    package.json

    package.json משמש לציון יחסי התלות של האפליקציה, גרסת הצומת והפעלת האפליקציה.

    {
      "name": "appengine-hello-world",
      "description": "Simple Hello World Node.js sample for Google App Engine Flexible Environment.",
      "version": "0.0.2",
      "private": true,
      "license": "Apache-2.0",
      "author": "Google Inc.",
      "repository": {
        "type": "git",
        "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
      },
      "engines": {
        "node": ">=20.0.0"
      },
      "scripts": {
        "start": "node app.js",
        "test": "c8 mocha -p -j 2 --exit test/*.test.js"
      },
      "dependencies": {
        "express": "^5.0.0"
      },
      "devDependencies": {
        "c8": "^12.0.0",
        "mocha": "^10.0.0",
        "supertest": "^7.0.0"
      }
    }
    

    בדוגמה שלמעלה, הקובץ package.json מציין שהאפליקציה משתמשת בגרסה 18 ומעלה של Node.js, והיא תלויה ב-express. במסמכי התיעוד של סביבת זמן הריצה של Node.js מוסבר איך להתקין ולהריץ את האפליקציה.