Node.js 適用的預訂伺服器架構
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
這是以 Node.js 為基礎的 API v3 標準整合預訂伺服器參考實作項目
必要條件
需要安裝
開始使用
為方便說明,本範例使用標準 Node.js 實作預訂伺服器,但不使用任何額外的程式庫或架構。如果您使用其他架構,可以輕鬆將此實作項目變更為 Express.js、MEAN.js 或任何其他您偏好的 Node.js 架構。
實作項目也不使用通訊協定緩衝區程式庫,而是仰賴簡單的 JSON 序列化,以及其 JSON.parse() 和 JSON.stringify() 方法。
如要下載專案,請執行下列指令:
git clone https://maps-booking.googlesource.com/js-maps-booking-rest-server-v3-skeleton
整個程式碼集只包含兩個 JavaScript 檔案:
- bookingserver.js:HTTP 伺服器和要求處理邏輯,包括驗證
- apiv3methods.js - 實作 API 第 3 版介面的程式
下載檔案後,您可以執行下列指令來啟動預訂伺服器:
node bookingserver.js
骨架會將所有傳入和傳出要求寫入控制台,方便您監控執行情形以便追蹤。
如果您需要 IDE 來變更程式碼或進行偵錯,可以使用 Visual Studio Code 或任何其他編輯器。在 Node.js 環境中啟動 bookingserver.js,並視需要設定中斷點,以便對專案進行偵錯。
測試預訂伺服器
下載預訂測試公用程式。如要安裝,請按照 README 頁面提供的安裝操作說明進行。
為了進行測試,您需要建立文字檔來儲存憑證。在單一行中輸入使用者名稱和密碼,例如在名為 cred.txt 的檔案中輸入:
username:password
你也需要測試商家的可用性動態饋給本機副本。在下方的範例指令中,供應情形動態饋給為 avail.json。
您現在可以使用下列指令測試預訂伺服器:
- 測試對 HealthCheck 方法的呼叫:
bin/bookingClient -server_addr="localhost:8080" -health_check_test=true -credentials_file="./cred.txt"
- 測試對 CheckAvailability 方法的呼叫:
bin/bookingClient -server_addr="localhost:8080" -check_availability_test=true -availability_feed="./avail.json" -credentials_file="./cred.txt"
- 測試對 CreateBooking 和 UpdateBooking 方法的呼叫:
bin/bookingClient -server_addr="localhost:8080" -booking_test=true -availability_feed="./avail.json" -credentials_file="./cred.txt"
由於您要自行實作預訂伺服器,因此可能需要針對該伺服器執行其他測試 (例如 list_bookings_test、rescheduling_test 等),目標是讓所有測試都通過 (-all_tests=true)。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間:2025-07-26 (世界標準時間)。"],[[["\u003cp\u003eThis is a Node.js-based reference implementation of the API v3 Booking Server for Standard Integration, allowing developers to understand and build their own booking systems.\u003c/p\u003e\n"],["\u003cp\u003eThe server utilizes basic Node.js functionalities and JSON for serialization, offering flexibility for adaptation to various frameworks like Express.js or MEAN.js.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can clone the provided repository, run the server using a simple command, and leverage debugging tools like Visual Studio Code for development.\u003c/p\u003e\n"],["\u003cp\u003eA Booking test utility and sample commands are provided for validating the functionality of the implemented Booking Server using credentials and an Availability feed.\u003c/p\u003e\n"],["\u003cp\u003eFurther testing with additional commands is encouraged to ensure comprehensive coverage and successful integration with the Booking API.\u003c/p\u003e\n"]]],["This document details a Node.js-based reference implementation of an API v3 Booking Server. It uses standard Node.js and JSON serialization. Key actions include: cloning the project via `git`, starting the server with `node bookingserver.js`, and utilizing the Booking test utility for testing. To test, users must create a credentials file and use an Availability feed. Testing commands are provided for HealthCheck, CheckAvailability, CreateBooking, and UpdateBooking. It encourages comprehensive testing.\n"],null,["# Booking Server Skeleton for Node.js\n\nThis is a reference implementation of\n[API v3 Booking Server for Standard Integration](/actions-center/verticals/local-services/e2e/integration-steps/implement-booking-server)\nbased on Node.js\n\n### Prerequisites\n\nRequires an installation of\n\n- [Node.js](https://nodejs.org/)\n\nGetting Started\n---------------\n\nThe Booking Server is implemented using standard Node.js without any\nadditional libraries or frameworks, for illustration purposes. If you are\nusing any other frameworks, you could easily change this implementation to\nExpress.js, MEAN.js, or any other Node.js-based framework of your choice.\n\nThe implementation is also not using protocol buffer libraries, but instead\nrelies on simple JSON serialization and its JSON.parse() and JSON.stringify()\nmethods.\n\nTo download the project, execute the following command: \n\n```javascript\ngit clone https://maps-booking.googlesource.com/js-maps-booking-rest-server-v3-skeleton\n```\n\nThe entire code base consists of only two JavaScript files:\n\n- bookingserver.js - HTTP server and requests handling logic, including authentication\n- apiv3methods.js - methods implementing API v3 interface\n\nAfter you download the files, you can start the Booking Server by running\nthe command: \n\n```javascript\nnode bookingserver.js\n```\n\nThe skeleton writes all incoming and outgoing requests to the console, so you\ncan monitor its execution for tracing purposes.\n\nShould you need an IDE for code changes or debugging, you can use\n[Visual Studio Code](https://code.visualstudio.com/) or any other\neditor of your choice. Debug the project by starting bookingserver.js in the\nNode.js environment and set breakpoints where needed.\n\nTesting your Booking Server\n---------------------------\n\nDownload\n[Booking test utility](https://maps-booking.googlesource.com/maps-booking-v3/). To install it, follow the provided installation\ninstructions in its README page.\n\nFor the tests, you need to create a text file to store your credentials.\nEnter your username and password on a single line, for example, in a file\nnamed cred.txt:\n\nusername:password\n\nYou also need a local copy of an Availability feed for your test merchants.\nIn the samples commands below, the Availability feed is avail.json.\n\nNow, you can test your Booking Server with these commands:\n\n- Test calls to HealthCheck method: \n\n ```javascript\n bin/bookingClient -server_addr=\"localhost:8080\" -health_check_test=true -credentials_file=\"./cred.txt\"\n ```\n- Test calls to CheckAvailability method: \n\n ```javascript\n bin/bookingClient -server_addr=\"localhost:8080\" -check_availability_test=true -availability_feed=\"./avail.json\" -credentials_file=\"./cred.txt\"\n ```\n- Test calls to CreateBooking and UpdateBooking methods: \n\n ```javascript\n bin/bookingClient -server_addr=\"localhost:8080\" -booking_test=true -availability_feed=\"./avail.json\" -credentials_file=\"./cred.txt\"\n ```\n\nAs you are implementing your own Booking Server, you may need to run\nadditional tests against it (e.g. list_bookings_test, rescheduling_test, etc)\nwith the goal of all tests passing (-all_tests=true)."]]