This guide shows how to write a Node.js web service to run in the App Engine standard environment. To learn more about the Node.js runtime and how it works, see Node.js Runtime Environment.
Before you begin
Key points
- You can use dependencies by listing them in your
package.jsonfile. See Specifying Dependencies for more information. - App Engine starts your application by running
npm start. - Your server must listen to the port specified by the
process.env.PORT, an environment variable set by the App Engine runtime. - You need an
app.yamlfile to deploy your service to App Engine.
Create a server to listen for HTTP requests
The core of your web service is the HTTP server. The sample code in this guide uses the Express.js framework to handle HTTP requests, but you are free to use a web framework of your choice.
Create a new folder called
my-nodejs-servicefor your Node.js service.Navigate to the folder in your terminal, and create a
package.jsonfile by runningnpm init.Add Express as a dependency by running:
npm install expressConfirm that Express appears in your
package.jsonfile'sdependenciesfield. Here's an example:{ ... "dependencies": { "express": "^4.16.3" } ... }Add a
startscript to yourpackage.jsonfile:"scripts": { "start": "node server.js" }Create a file called
server.jsin the same folder and add the following code:
This is a very basic web server - it responds to all GET requests to the root
path ('/') with the text "Hello from App Engine!" Note the last four lines,
where the server is set to listen to the port specified by process.env.PORT,
an environment variable
set by the App Engine runtime. If your server isn't set to listen to
this port, it will not receive requests.
Notice that if process.env.PORT is not set, port 8080 is used as
a default. This is necessary for testing your app locally, because
process.env.PORT doesn't get set during local runs - it is only set when
your app is running on App Engine. You can use whichever port
you prefer for testing, but this guide uses 8080.
Run the server locally
To run the server locally:
Run
npm startin your terminal. This will run yourserver.jsfile.Point your web browser to
http://localhost:8080.
You should see a page with the text "Hello from App Engine!"
Create the app.yaml file
An app.yaml file specifies settings for your App Engine service's
runtime environment. Your service will not deploy without this file.
In your
my-nodejs-servicefolder, create a file calledapp.yaml.Add the following contents:
This is a minimal configuration file, indicating to App Engine the version of the Node.js runtime. The
app.yamlfile can also specify network settings, scaling settings, and more. For more information, see theapp.yamlreference.
At this point, you should have a file structure like the following:
my-nodejs-service/
app.yaml
package.json
server.js
Next steps
Now that you've created a simple Node.js web server that listens to the correct
port and you've specified the runtime in an app.yaml file, you're ready to
deploy your service on App Engine.
Try it for yourself
If you're new to Google Cloud, create an account to evaluate how App Engine performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
Try App Engine free