---
# System prepended metadata

title: NodeJS tutorial

---

# NodeJS tutorial

### Prepare - MySQL
1. install mysql 
   ```
   wget https://dev.mysql.com/get/mysql-apt-config_0.8.10-1_all.deb
   sudo dpkg -i mysql-apt-config_0.8.10-1_all.deb
   sudo apt-get update
   sudo apt-get install mysql-server
   mysql_secure_installation
   ```
2. Create user
   ```
   $ mysql -u root -p
   # enter your password, you press enter directly in first time
   
   mysql> CREATE USER 'pj'@'%' IDENTIFIED BY 'password';
   mysql> GRANT ALL PRIVILEGES ON *.* TO 'pj'@'%';
   ```
3. Create database
   ```
   mysql> CREATE DATABASE wedding;
   ```
4. Create table
   ```
   mysql> CREATE TABLE forms(
             id BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL,
             name VARCHAR(255) NOT NULL,
             email VARCHAR(255) NOT NULL,
             phone VARCHAR(255) NOT NULL,
             others TEXT
          );

### Prepare - Respository
1. clone repository
    ```
    git clone https://github.com/pjwang0710/node_express_template.git
    ```

### Prepare - Environment
1. Create .env file
   ```
   $ cd node_express_template
   $ vi .env
   ```
2. Write `.env` file
   ```
   API_VERSION = '1.0'
   NODE_ENV = 'development'
   PORT = 3000
   DB_HOST = 'localhost'
   DB_USERNAME = 'pj'       # set your user name
   DB_PASSWORD = 'password' # set your password
   DB_DATABASE = 'wedding'  # set your db name
   ```
3. install package
   ```
   $ npm i
   ```

### Prepare - pm2 
1. install pm2
   ```
   $ npm install pm2 -g
   ```


### RUN
1. use `pm2` run `app.js`
   ```
   $ pm2 start app.js
   ```
2. check your whole processes which are run by `pm2`
   ```
   $ pm2 list
   ```
3. check the log from single process
   ```
   $ pm2 log 0
   ```

### SET nginx
1. Modify your config
   ```
   $ sudo vi /etc/nginx/sites-enabled/default
   ```
2. Add setting
   ```
   server {
        listen 80;
        listen [::]:80;
        listen 443;
        listen [::]:443;

        server_name api.DOMAIN.com;

        location / {
                proxy_pass http://0.0.0.0:3000/;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

### Frontend setting
#### APIs
GET `https://api.DOMAIN.com/api/1.0/forms/getForms`
Response:
```
{
  "data": [
    [
      {
        "id": 1,
        "name": "pj",
        "email": "daasdsad@gmail.com",
        "phone": "0937123456",
        "others": "NO"
      },
      {
        "id": 2,
        "name": "daniel",
        "email": "pjw777888@gmail.com",
        "phone": "0937123456",
        "others": "Others"
      },
      {
        "id": 3,
        "name": "test",
        "email": "test@gmail.com",
        "phone": "0987654321",
        "others": "Others"
      },
      {
        "id": 4,
        "name": "test123",
        "email": "test789@gmail.com",
        "phone": "0987654321",
        "others": "Others"
      }
    ]
  ]
}
```

POST `https://api.DOMAIN.com/api/1.0/forms/insertForm`
Request:
```
{
    "name": "test123",
    "phone": "0987654321",
    "email": "test789@gmail.com",
    "others": "Others"
}
```
Response:
```
{
    "data": {}
}
```