Skip to content

Commit 3d82eed

Browse files
authored
Merge pull request #301 from DefangLabs/linda-nodejs
Update nodejs samples
2 parents cba5707 + 55ae8d1 commit 3d82eed

File tree

9 files changed

+304
-59
lines changed

9 files changed

+304
-59
lines changed

samples/nodejs-chatroom/README.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,44 @@ This is a minimal chat application that shows how to use Socket.IO with Node.js
66

77
## Prerequisites
88

9-
Install the Defang CLI by following the instructions in the [Defang CLI documentation](https://docs.defang.io/docs/getting-started).
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
1012

11-
## Build and run the application
13+
## Development
1214

13-
If you have environment variables configured for your [own cloud account](https://docs.defang.io/docs/concepts/defang-byoc), this will deploy the application to your cloud account, otherwise it will deploy to the Defang cloud.
15+
To run the application locally, you can use the following command:
1416

15-
```sh
17+
```bash
18+
docker compose up --build
19+
```
20+
21+
## Configuration
22+
23+
For this sample, you will not need to provide [configuration](https://docs.defang.io/docs/concepts/configuration).
24+
25+
If you wish to provide configuration, see below for an example of setting a configuration for a value named `API_KEY`.
26+
27+
```bash
28+
defang config set API_KEY
29+
```
30+
31+
## Deployment
32+
33+
> [!NOTE]
34+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
35+
36+
### Defang Playground
37+
38+
Deploy your application to the Defang Playground by opening up your terminal and typing:
39+
```bash
1640
defang compose up
1741
```
1842

43+
### BYOC (AWS)
44+
45+
If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud)
46+
1947
---
2048

2149
Title: Node.js & SocketIO

samples/nodejs-express/README.md

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,46 @@
44

55
This Node.js application, built with Express.js, is designed to inspect and display detailed information about incoming HTTP requests. It supports all HTTP methods and provides insights into the request path, method, headers, query parameters, and body. Note alongside your project, you should also include a package.json file that includes the relevant metadata such as package dependencies, scripts, project verrsions so that the Dockerfile can install necessary dependencies.
66

7-
## Essential Setup Files
7+
## Prerequisites
88

9-
1. A [Dockerfile](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/).
10-
2. A [compose file](https://docs.defang.io/docs/concepts/compose) to define and run multi-container Docker applications (this is how Defang identifies services to be deployed).
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
1112

12-
## Prerequisite
13+
## Development
1314

14-
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
15-
2. If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc), make sure you have properly [authenticated your AWS account (optional)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
15+
To run the application locally, you can use the following command:
16+
17+
```bash
18+
docker compose up --build
19+
```
20+
21+
## Configuration
22+
23+
For this sample, you will not need to provide [configuration](https://docs.defang.io/docs/concepts/configuration).
24+
25+
If you wish to provide configuration, see below for an example of setting a configuration for a value named `API_KEY`.
26+
27+
```bash
28+
defang config set API_KEY
29+
```
30+
31+
## Deployment
32+
33+
> [!NOTE]
34+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
35+
36+
### Defang Playground
37+
38+
Deploy your application to the Defang Playground by opening up your terminal and typing:
39+
```bash
40+
defang compose up
41+
```
42+
43+
### BYOC (AWS)
1644

17-
## A Step-by-Step Guide
45+
If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud).
1846

19-
1. Open the terminal and type `defang login`
20-
2. Type `defang compose up` in the CLI
21-
3. Your app should be up and running with Defang in minutes!
2247

2348
---
2449

samples/nodejs-form/README.md

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,48 @@
22

33
[![1-click-deploy](https://defang.io/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-nodejs-form-template%26template_owner%3DDefangSamples)
44

5-
This is a basic Node.js application using the Express framework to demonstrate handling a form submission. The application serves an HTML form where users can input their first name and then greets them personally upon submission. Note alongside your project, you should also include a package.json file that includes the relevant metadata such as package dependencies, scripts, project verrsions so that the Dockerfile can install necessary dependencies.
5+
This is a basic Node.js application using the Express framework to demonstrate handling a form submission. The application serves an HTML form where users can input their first name and then greets them personally upon submission.
66

7-
## Essential Setup Files
7+
## Prerequisites
88

9-
1. A [Dockerfile](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/).
10-
2. A [compose file](https://docs.defang.io/docs/concepts/compose) to define and run multi-container Docker applications (this is how Defang identifies services to be deployed).
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
1112

12-
## Prerequisite
13+
## Development
1314

14-
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
15-
2. If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc), make sure you have properly [authenticated your AWS account (optional)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
15+
To run the application locally, you can use the following command:
16+
17+
```bash
18+
docker compose up --build
19+
```
20+
21+
## Configuration
22+
23+
For this sample, you will not need to provide [configuration](https://docs.defang.io/docs/concepts/configuration).
24+
25+
If you wish to provide configuration, see below for an example of setting a configuration for a value named `API_KEY`.
26+
27+
```bash
28+
defang config set API_KEY
29+
```
30+
31+
## Deployment
32+
33+
> [!NOTE]
34+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
35+
36+
### Defang Playground
37+
38+
Deploy your application to the Defang Playground by opening up your terminal and typing:
39+
```bash
40+
defang compose up
41+
```
42+
43+
### BYOC (AWS)
1644

17-
## A Step-by-Step Guide
45+
If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud):
1846

19-
1. Open the terminal and type `defang login`
20-
2. Type `defang compose up` in the CLI
21-
3. Your app should be up and running with Defang in minutes!
2247

2348
---
2449

samples/nodejs-http/README.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,44 @@ This example shows how to build a minimal Node.js application using the [Node.js
66

77
## Prerequisites
88

9-
Install the Defang CLI by following the instructions in the [Defang CLI documentation](https://docs.defang.io/docs/getting-started).
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
1012

11-
## Build and run the application
13+
## Development
1214

13-
If you have environment variables configured for your [own cloud account](https://docs.defang.io/docs/concepts/defang-byoc), this will deploy the application to your cloud account, otherwise it will deploy to the Defang cloud.
15+
To run the application locally, you can use the following command:
1416

15-
```sh
17+
```bash
18+
docker compose up --build
19+
```
20+
21+
## Configuration
22+
23+
For this sample, you will not need to provide [configuration](https://docs.defang.io/docs/concepts/configuration).
24+
25+
If you wish to provide configuration, see below for an example of setting a configuration for a value named `API_KEY`.
26+
27+
```bash
28+
defang config set API_KEY
29+
```
30+
31+
## Deployment
32+
33+
> [!NOTE]
34+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
35+
36+
### Defang Playground
37+
38+
Deploy your application to the Defang Playground by opening up your terminal and typing:
39+
```bash
1640
defang compose up
1741
```
1842

43+
### BYOC (AWS)
44+
45+
If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud):
46+
1947
---
2048

2149
Title: Node.js HTTP Server

samples/nodejs-openai/README.md

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,62 @@
22

33
[![1-click-deploy](https://defang.io/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-nodejs-openai-template%26template_owner%3DDefangSamples)
44

5-
## Setup
5+
A simple Node.js and OpenAI app deployed using Defang.
66

7-
This sample requires an API key to access the OpenAI API. The name of the config value is referenced in the compose.yaml file.
8-
To provide a value for it, you can use the Defang CLI like this:
7+
## Prerequisites
98

10-
```
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
12+
13+
## Development
1114

12-
defang config set --name OPENAI_KEY
15+
To run the application locally, you can use the following command:
16+
17+
```bash
18+
docker compose up --build
1319
```
1420

15-
and then enter the value when prompted.
21+
## Configuration
22+
For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration):
1623

17-
## Testing
24+
> Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
1825
26+
### `OPENAI_KEY`
27+
An API key to access the OpenAI API.
28+
```bash
29+
defang config set OPENAI_KEY
1930
```
31+
32+
## Testing
33+
Below are some useful commands for testing.
34+
35+
```bash
2036
echo "Hello" | curl -H "Content-Type: text/plain" -d @- https://xxxxxxxx/prompt
2137
```
2238

23-
or
39+
or alternatively,
2440

25-
```
41+
```bash
2642
cat prompt.txt | curl -H "Content-Type: application/text" -d @- https://xxxxxxxx/prompt
2743
```
2844

45+
## Deployment
46+
47+
> [!NOTE]
48+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
49+
50+
### Defang Playground
51+
52+
Deploy your application to the Defang Playground by opening up your terminal and typing:
53+
```bash
54+
defang compose up
55+
```
56+
57+
### BYOC (AWS)
58+
59+
If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud):
60+
2961
---
3062

3163
Title: Node.js & OpenAI

samples/nodejs-react-postgres/README.md

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,58 @@
22

33
[![1-click-deploy](https://defang.io/deploy-with-defang.svg)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-nodejs-react-postgres-template%26template_owner%3DDefangSamples)`
44

5-
This sample project demonstrates how to deploy a full-stack application using React for the frontend, Node.js for the backend, and PostgreSQL for the database. The project uses Docker to containerize the services, making it easy to run in both development and production environments.
5+
This sample project demonstrates how to deploy a full-stack application using React for the frontend, Node.js for the backend, and PostgreSQL for the database.
66

77
In this sample, we have set up the essential files you need to deploy in production using [Neon](https://neon.tech/) to host your database. We use a connection string to connect Neon to your code. By replacing the pre-configured connection string at .env and at the compose file to yours, you will be ready to deploy this sample with Neon.
88

9-
## Essential Setup Files
9+
## Prerequisites
1010

11-
1. Download [Defang CLI] (https://github.com/defang-io/defang)
12-
2. (optional) If you are using [Defang BYOC] (https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) authenticated your AWS account.
13-
3. (optional for local development) [Docker CLI] (https://docs.docker.com/engine/install/)
11+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
12+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
13+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
1414

1515
## Development
1616

17-
To start the development environment, run `docker compose -f ./compose.yaml -f ./compose.dev.yaml up`. This will start the Postgres container, the React container, and the NodeJS container. The development container (compose.dev.yaml) will override the production container (compose.yaml).
17+
To run the application locally, you can use the following command:
1818

19+
```bash
20+
docker compose -f compose.dev.yaml up --build
21+
```
1922
Or run without using Docker by doing the following:
2023

21-
1. run npm install to install the nodejs dependencies in both the `client` directory and the `server` directory
24+
1. run `npm install` to install the nodejs dependencies in both the `client` directory and the `server` directory
2225
2. create or modify the .env file in both the `client` directory and the `server` directory with localhost, or create a .env.local to override the .env file.
23-
3. run npm start
26+
3. run `npm start`
2427

25-
## Deploying
28+
## Configuration
29+
For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration):
2630

27-
1. Open the terminal and type `defang login`
28-
2. Add your connection string as a defang config value by typing `defang config set DATABASE_URL` and pasting your connection string (which should be in the format `postgres://username:password@host:port/dbname`)
29-
3. Update your `compose.yaml` file to replace `<YOUR_USERNAME>` with your username (which you can get by running `defang whoami`. "Tenant" is your username.)
30-
4. Type `defang compose up` in the CLI.
31-
5. Your app will be running within a few minutes.
31+
> Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
32+
33+
### `DATABASE_URL`
34+
A connection to the database, which should be in the format of `postgres://username:password@host:port/dbname`.
35+
```bash
36+
defang config set DATABASE_URL
37+
```
38+
39+
### `REACT_APP_API_URL`
40+
A URL for the React App. For this, you will need to update the `compose.yaml` file to replace `<YOUR_USERNAME>` with your username, which you can get by running `defang whoami`.
41+
42+
## Deployment
43+
44+
> [!NOTE]
45+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
46+
47+
### Defang Playground
48+
49+
Deploy your application to the Defang Playground by opening up your terminal and typing:
50+
```bash
51+
defang compose up
52+
```
53+
54+
### BYOC (AWS)
55+
56+
If you want to deploy to your own cloud account, you can [use Defang BYOC](https://docs.defang.io/docs/tutorials/deploy-to-your-cloud):
3257

3358
---
3459

samples/nodejs-react-postgres/compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services:
55
context: ./client
66
dockerfile: Dockerfile
77
args:
8-
- REACT_APP_API_URL=https://<YOUR_USERNAME>-server--3010.prod1.defang.dev
8+
- REACT_APP_API_URL=https://<YOUR_USERNAME>-server--3010.prod1.defang.dev #replace <YOUR_USERNAME> with your username
99
ports:
1010
- mode: ingress
1111
target: 3000

0 commit comments

Comments
 (0)