# Create and deploy an HTTP Cloud Run function by using
Node.js (1st gen)

This guide takes you through the process of writing a Cloud Run function
using the Node.js runtime. There are two types of Cloud Run functions:

- An HTTP function, which you invoke from standard HTTP requests.
- An event-driven function, which you use to handle events from your Cloud infrastructure, such as messages on a Pub/Sub topic, or changes in a Cloud Storage bucket.

The sample shows how to create a simple HTTP function.

> [!IMPORTANT]
> **Learn more** : For more details, read about [HTTP functions](https://docs.cloud.google.com/functions/1stgendocs/writing/write-http-functions.md.txt) and [event-driven functions](https://docs.cloud.google.com/functions/1stgendocs/writing/write-event-driven-functions.md.txt).

## Before you begin

1. Prepare your development environment.
   [Go to the Node.js setup guide](https://docs.cloud.google.com/nodejs/docs/setup.md.txt)

<br />

## Create a function

1. Create a directory on your local system for the function code:

   ### Linux or Mac OS X

       mkdir ~/helloworld
       cd ~/helloworld

   ### Windows

       mkdir %HOMEPATH%\helloworld
       cd %HOMEPATH%\helloworld

2. Create an `index.js` file in the `helloworld` directory with the
   following contents:

   ```javascript
   const functions = require('@google-cloud/functions-framework');
   const escapeHtml = require('escape-html');

   /**
    * Responds to an HTTP request using data from the request body parsed according
    * to the "content-type" header.
    *
    * @param {Object} req Cloud Function request context.
    * @param {Object} res Cloud Function response context.
    */
   functions.http('helloHttp', (req, res) => {
     const name =
       (req.query && req.query.name) || (req.body && req.body.name) || 'World';

     res.send(`Hello ${escapeHtml(name)}!`);
   });
   ```

   This example function takes a name supplied in the HTTP request and returns
   a greeting, or "Hello World!" when no name is supplied.

   > [!NOTE]
   > **Note:** Cloud Run functions looks for deployable functions in `index.js` by default. Use the [`--source`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/deploy.md.txt#--source) flag when [deploying your function via `gcloud`](https://docs.cloud.google.com/functions/1stgendocs/create-deploy-http-nodejs-1st-gen.md.txt#deploying_the_function) to specify a different directory containing an `index.js` file.

## Specify dependencies

Dependencies in Node.js are stored in a file called
[`package.json`](https://docs.npmjs.com/files/package.json).
You can create this file manually or with the
[npm](https://docs.npmjs.com/) command.

- To create your `package.json` dependency file by using `npm`, run these
  commands:

      npm init
      npm install c8 gaxios mocha sinon supertest wait-port --save-dev
      npm install @google-cloud/functions-framework escape-html

- If you prefer to build your `package.json`file manually, create a `package.json`
  file in the `helloworld` directory with the following contents:

  ```javascript
  {
    "name": "nodejs-docs-samples-functions-hello-world-http",
    "version": "0.0.1",
    "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": {
      "unit-test": "c8 mocha -p -j 2 test/index.test.js test/*unit*test.js test/*integration*test.js --timeout=6000 --exit",
      "system-test": "c8 mocha -p -j 2 test/*system*test.js --timeout=600000 --exit",
      "all-test": "npm run unit-test && npm run system-test",
      "test": "npm -- run unit-test"
    },
    "dependencies": {
      "@google-cloud/functions-framework": "^5.0.0",
      "escape-html": "^1.0.3"
    },
    "devDependencies": {
      "c8": "^12.0.0",
      "gaxios": "^7.0.0",
      "mocha": "^10.0.0",
      "sinon": "^18.0.0",
      "supertest": "^7.0.0",
      "wait-port": "^1.0.4"
    }
  }
  ```

  > [!NOTE]
  > **Note:** Node.js client libraries are available for a variety of Google Cloud products, and can be installed as dependencies. See the list [here](https://github.com/googleapis/google-cloud-node#google-cloud-nodejs-client-libraries).

> [!IMPORTANT]
> **Learn more** : For more details, read about [specifying dependencies](https://docs.cloud.google.com/functions/1stgendocs/writing/specifying-dependencies-nodejs.md.txt).

## Deploy the function

To deploy the function with an HTTP trigger, run the following
command in the `helloworld` directory:

    gcloud functions deploy helloHttp --no-gen2 --runtime nodejs22 --trigger-http --allow-unauthenticated

The `--allow-unauthenticated` flag lets you reach the function
[without authentication](https://docs.cloud.google.com/functions/1stgendocs/securing/managing-access-iam.md.txt#allowing_unauthenticated_http_function_invocation).
To require
[authentication](https://docs.cloud.google.com/functions/1stgendocs/securing/authenticating.md.txt#developers), omit the
flag.

> [!IMPORTANT]
> **Learn more** : For more details, read about [deploying
> Cloud Run functions](https://docs.cloud.google.com/functions/1stgendocs/deploy#basics).

## Test the function

1. When the function finishes deploying, take note of the `httpsTrigger.url`
   property or find it using the following command:

       gcloud functions describe helloHttp

   It should look like this:

   ```
   https://GCP_REGION-PROJECT_ID.cloudfunctions.net/helloHttp
   ```
2. Visit this URL in your browser. You should see a "Hello World!" message.

   Try passing a name in the HTTP request, for example by using the following
   URL:

   ```
   https://GCP_REGION-PROJECT_ID.cloudfunctions.net/helloHttp?name=NAME
   ```

   You should see the message "Hello `NAME`!"

## View logs

Logs for Cloud Run functions are viewable using the Google Cloud CLI, and in the
Cloud Logging UI.

### Use the command-line tool

To view logs for your function with the gcloud CLI, use the
[`logs read`](https://docs.cloud.google.com/sdk/gcloud/reference/functions/logs/read) command, followed by
the name of the function:

```bash
gcloud functions logs read helloHttp
```

The output should resemble the following:

```bash
LEVEL  NAME       EXECUTION_ID  TIME_UTC                 LOG
D      helloHttp  rvb9j0axfclb  2019-09-18 22:06:25.983  Function execution started
D      helloHttp  rvb9j0axfclb  2019-09-18 22:06:26.001  Function execution took 19 ms, finished with status code: 200
```

> [!NOTE]
> **Note:** There is typically a slight delay between when log entries are created and when they show up in Cloud Logging.

### Use the Logging dashboard

You can also view logs for Cloud Run functions from the
[Google Cloud console](https://console.cloud.google.com/project/_/logs?service=cloudfunctions.googleapis.com).

> [!IMPORTANT]
> **Learn more** : For more details, read about [writing, viewing,
> and responding to logs](https://docs.cloud.google.com/functions/1stgendocs/monitoring/logging).