Run Express.js on App Engine flexible environment
Author(s):
@jmdobry
,
Published: 2016-01-07
Contributed by Google employees.
You can check out Node.js and Google Cloud to get an overview of Node.js itself and learn ways to run Node.js apps on Google Cloud.
Prerequisites
- Create a project in the Cloud Console.
- Enable billing for your project.
- Install the Cloud SDK.
- Install Node.js on your local machine.
Prepare
Initialize a
package.json
file with the following command:npm init
Add a start script to your
package.json
file:"scripts": { "start": "node index.js" }
Install Express.js:
npm install --save express
Create
Create an index.js
file with the following contents:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
const server = app.listen(8080, () => {
const host = server.address().address;
const port = server.address().port;
console.log(`Example app listening at http://${host}:${port}`);
});
Run
Run the app with the following command:
npm start
Visit http://localhost:8080 to see the
Hello World!
message.
Deploy
Create an
app.yaml
file with the following contents:runtime: nodejs env: flex
Run the following command to deploy your app:
gcloud app deploy
Visit
http://YOUR_PROJECT_ID.appspot.com
to see theHello World!
message.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see our Site Policies. Java is a registered trademark of Oracle and/or its affiliates.