Skip to content

Commit 693e0c6

Browse files
authored
Redis Express App (#65)
* redis-app Signed-off-by: akshat99812 <138353837+akshat99812@users.noreply.github.com> --------- Signed-off-by: akshat99812 <138353837+akshat99812@users.noreply.github.com>
1 parent d8376ba commit 693e0c6

9 files changed

Lines changed: 1617 additions & 0 deletions

File tree

redis-express/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# node modules
2+
node_modules
3+
4+
# vs code
5+
6+
.DS_Store

redis-express/Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Downloading node:alpine
2+
FROM node:alpine
3+
4+
WORKDIR /redis-ts
5+
6+
COPY package*.json ./
7+
8+
RUN npm install
9+
10+
COPY . .
11+
12+
RUN npm run build
13+
14+
# Download the ca.crt file and setup_ca.sh script
15+
RUN curl -o ca.crt https://raw.githubusercontent.com/keploy/keploy/main/pkg/core/proxy/asset/ca.crt
16+
RUN curl -o setup_ca.sh https://raw.githubusercontent.com/keploy/keploy/main/pkg/core/proxy/asset/setup_ca.sh
17+
18+
# Give execute permission to the setup_ca.sh script
19+
RUN chmod +x setup_ca.sh
20+
21+
# Expose the port your app runs on
22+
EXPOSE 3000
23+
24+
25+
# Define the command to run your application with CA setup
26+
CMD ["/bin/bash", "-c", "source ./setup_ca.sh && node dist/index.js"]

redis-express/README.md

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
2+
# Express Redis Sample
3+
4+
## Overview
5+
6+
This API manages books record, built with Express and Redis. It provides routes to add, update, delete, view books, view books by ID, and change task authors.
7+
8+
## Installation
9+
10+
### Prerequisites
11+
12+
Ensure you have the following installed:
13+
14+
- Docker
15+
- Node.js and npm
16+
- Keploy CLI
17+
18+
# Installing Keploy
19+
20+
Let's get started by setting up the Keploy alias with this command:
21+
22+
```sh
23+
curl -O https://raw.githubusercontent.com/keploy/keploy/main/keploy.sh && source keploy.sh
24+
```
25+
26+
Clone the repository and move to express-redis folder
27+
28+
```bash
29+
git clone https://github.com/keploy/samples-typescript.git
30+
cd samples-typescript/express-redis
31+
```
32+
## Installing Locally
33+
34+
### Install the dependencies
35+
```bash
36+
npm install
37+
```
38+
39+
### Start Redis-server Container
40+
```bash
41+
docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest
42+
```
43+
44+
### Load the Samples and build server:
45+
46+
In case of running application natively, we will have to change the host to localhost in index.ts
47+
48+
```bash
49+
const redisClient = new Redis({
50+
```
51+
52+
If running server on docker
53+
54+
```bash
55+
host: 'redis',
56+
```
57+
58+
If running server locally
59+
60+
```bash
61+
host: 'localhost'
62+
port: 6379
63+
```
64+
65+
### Build the application:
66+
```bash
67+
npm install
68+
npm run build
69+
```
70+
71+
### Start the application:
72+
```bash
73+
npm run start
74+
```
75+
76+
77+
> redis-ts@1.0.0 start
78+
> node dist/index.js
79+
80+
81+
```bash
82+
Server is running on http://localhost:3000
83+
Connected to Redis server
84+
Sample books loaded into Redis
85+
```
86+
87+
Some sample data is automatically created in redis.
88+
89+
90+
## Using Docker
91+
92+
Make sure you have docker installed.
93+
94+
```bash
95+
git clone https://github.com/keploy/samples-typescript.git
96+
cd samples-typescript/express-redis
97+
docker compose up
98+
```
99+
100+
### Generate Test Cases
101+
102+
For server running locally.
103+
```bash
104+
keploy record -c "npm run start"
105+
```
106+
107+
For the docker container.
108+
109+
```bash
110+
keploy record -c "docker compose up" --container-name "redis-express" -n "keploy-network"
111+
```
112+
113+
The above command will start recording the API calls made to the application and will generate a test case in the `testcases/` directory.
114+
115+
> 💡 You can use Postman ,CURL or any other API testing tool to test the API calls.
116+
117+
### Interact with Application
118+
119+
Use Postman, CURL, or any other API testing tool to hit your API routes. This interaction will be recorded by Keploy.
120+
121+
Some curl commands to easily test the application are
122+
123+
1. "http://localhost:3000/books" to retrieve the data of the books
124+
125+
```bash
126+
curl --location 'http://localhost:3000/books'
127+
```
128+
129+
2. "http://localhost:3000/books/:id" to retrieve the data of a specific book
130+
131+
```bash
132+
curl --location 'http://localhost:3000/books/2'
133+
```
134+
135+
3. "http://localhost:3000/publish" to publish a book
136+
137+
```bash
138+
curl --location 'http://localhost:3000/publish' --header 'Content-Type: application/json' --data '{
139+
"id":"6",
140+
"title":"Harry Potter and the Prisoner of Azkaban",
141+
"author":"J.K. Rowling"
142+
}'
143+
```
144+
145+
4. "http://localhost:3000/books/:id" to delete a book
146+
147+
```bash
148+
curl --location --request DELETE 'http://localhost:3000/books/1'
149+
```
150+
151+
5. "http://localhost:3000/books/:id" to update a book
152+
153+
```bash
154+
curl --location --request PUT 'http://localhost:3000/books/6' --header 'Content-Type: application/json' --data '{
155+
"title":"Harry Potter and the Half-Blood Prince",
156+
"author":"J.K. Rowling"
157+
}'
158+
```
159+
160+
Observe test run results:
161+
Voila!! Our test cases have passed 🌟
162+
163+
### Test the Application using Keploy
164+
165+
For server running locally.
166+
```bash
167+
keploy test -c "npm run start"
168+
```
169+
170+
For the docker container.
171+
172+
```bash
173+
keploy test -c "docker compose up" --container-name "redis-express" -n "keploy-network"
174+
```
175+
176+
Keploy will replay the recorded interactions and validate the responses against the expected results.
177+
178+
Voila! 🎉 You have successfully tested the application using Keploy. Keploy also generates coverage reports for the test-suites.

redis-express/docker-compose.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: '3.8'
2+
3+
services:
4+
app:
5+
container_name: redis-express
6+
build: .
7+
ports:
8+
- "3000:3000"
9+
depends_on:
10+
- redis
11+
networks:
12+
- keploy-network
13+
14+
redis:
15+
image: redis/redis-stack-server:latest
16+
ports:
17+
- "6379:6379"
18+
networks:
19+
- keploy-network
20+
21+
networks:
22+
keploy-network:
23+
external: true

0 commit comments

Comments
 (0)