Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
APP_PORT=5000
# MySQL variables
MYSQL_ROOT_PASSWORD=replace-root-pwd
MYSQL_DATABASE=replace-db-name
MYSQL_USER=replace-db-user
MYSQL_PASSWORD=replace-db-pwd
MYSQL_PORT=3306
# make sure the MYSQL_SERVICE_IP matches with the one corresponding to the mysql container in the docker-compose.yml file
MYSQL_SERVICE_IP=172.19.0.2

# app variables
APP_PORT=5000
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
node_modules
.env
/mysql-data/*
!/mysql-data/dont-delete-folder__.md
155 changes: 155 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Express API

[ref](https://odyssey.wildcodeschool.com/quests/395)

### Create `.env` file

1. install `dotenv`

```bash
npm install dotenv
```

2. create `.env` file

This file must not be pushed to GitHub but you can include a .env.sample code to share indications on what to declare.

If you cloned a repo with such a file you can use :

```bash
cp .env.sample .env
```

modify the existing variables accordingly and add the `.env`in your `.gitignore`.

3. add below line at the top of `index.js`

```js
require("dotenv").config();
```

4. consume variables as follows

```js
const port = process.env.APP_PORT;
```

5. your environment is set up, run with `npm run dev`

### Create the database (via docker compose)

1. prepare the files

Make sure you have docker installed and update the MYSQL related variables in the `.env` file.

Data from `express_quests.sql` database file will be mounted into the container and the updated data will be persisted in the mysql-data file.

Refer to the `docker-compose.yml` file if you want to update these settings.


2. create mysql container

Create the container by running the below in the terminal :

```bash
docker compose up -d
```

You can access the CLI and check everything is in order with this command :

```bash
docker exec -it mysql-express-container mysql -u root -p
```

### Install MySQL 2 module

https://www.npmjs.com/package/mysql2

```bash
npm install mysql2
```

### Configure database access

1. Create a `database.js` file next to `index.js`

2. Require the `dotenv` package

Make sure you require the dotenv package at the very top.

```js
require("dotenv").config();
```

3. import `mysql2` package

4. Use `mysql.createPool` to prepare a connection

see `database.js` file


5. test connection

in `database.js`

```js
database
.getConnection()
.then(() => {
console.log("Can reach database");
})
.catch((err) => {
console.error(err);
});
```

and run the following command :

```bash
npx nodemon database.js
```

6. don't forget do include `module.exports = database;` at the end of `database.js`

### Postman

Postman is a great and powerful tool that we can use to test our routes.

1. Install Postman

on ubuntu use the command :
```bash
snap install postman
```
for other os check [here](https://learning.postman.com/docs/getting-started/installation/installation-and-updates/#install-postman-on-linux)

to launch postman on ubuntu :

```bash
# run in the background
postman &

# bring to the foreground
fg

# get back to the backgroung
# first suspend with `ctrl + Z` then
bg

# prevent background process from being terminated
# when you close the terminal
nohup postman &

```

### Test lifecycle management

The connection to the database prevents our test script from completing its execution, so it will have to be cut after all the tests have been executed.

Add the following to the test files :

```bash
const database = require("../database")

afterAll(() => database.end());
```
23 changes: 23 additions & 0 deletions database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require("dotenv").config();

const mysql = require("mysql2/promise");

const database = mysql.createPool({
host: process.env.MYSQL_SERVICE_IP, // address of the server
port: process.env.MYSQL_PORT, // port of the DB server (mysql), not to be confused with the APP_PORT !
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
});

// test connection
database
.getConnection()
.then(() => {
console.log("Can reach database");
})
.catch((err) => {
console.error(err);
});

module.exports = database;
22 changes: 22 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3.8'

services:
mysql-express-container:
image: mysql:latest
container_name: mysql-express-container
env_file:
- .env
ports:
- "${MYSQL_PORT:-3306}:3306"
volumes:
- ./express_quests.sql:/docker-entrypoint-initdb.d/express_quests.sql
- ./mysql-data:/var/lib/mysql
networks:
your_network_name:
ipv4_address: 172.19.0.2

networks:
your_network_name:
ipam:
config:
- subnet: 172.19.0.0/16
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require("dotenv").config();
const app = require("./src/app");

const port = 5000;
const port = process.env.APP_PORT;

app
.listen(port, () => {
Expand Down
1 change: 1 addition & 0 deletions mysql-data/dont-delete-folder__.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This folder is where the data of the mysql container is persisted, don't delete it.
Loading