Prerequisite
-
Install node
-
Install yarn
-
Install XAMPP For Running Local Mysql Server
-
Create a file named
.env
in the root directory & copy the texts from.env.example
-
Create a MySql DB & change the value of database in
.env
How to run without docker
-
Go to the project root directory
-
Run
yarn install
on terminal/CMD -
Then run
yarn global add sequelize-cli nodemon
on terminal/CMD -
Then run migrations. The commands for migration & creating model are listed below in DB Commands section
-
Go back to root directory and run
yarn start
-
Now your backend will be running on localhost:3000
DB Commands
- Open XAMPP/MAMP and start mysql service
- Add user root with any host in mysql :
CREATE USER 'root_any'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root_any'@'%';
- Edit database config in .env
- If you running with docker. Dont forget to change database host to :
host.docker.internal
If you running without docker using yarn start, change host to :
localhost
- For migration, run
sequelize db:migrate
- For creating a model, run
sequelize model:create --name TableName --attributes columnName:string, ...
All other sequelize commands are available here Sequelize Documentation
Running on Dockerfile only
The Dockerfile is also included. You need to just build for production with live mysql credential using :
Build with dockerfile
docker build -t express-sequelize-mysql .
Build with custom dockerfile.test
docker build -f Dockerfile.test -t express-sequelize-mysql .
Run
docker run --rm -it -p 3000:3000 express-sequelize-mysql
Open URL in browser : http://localhost:3000
Note : You can change docker name {express-sequelize-mysql} with your own
Production
Run not in background:
docker-compose up -t express-sequelize-mysql
Run in background
docker-compose up -t express-sequelize-mysql --build --force-recreate --no-deps -d
Local development
- The service can be run on the background with command :
docker-compose -f docker-compose.dev.yml up --build --force-recreate --no-deps -d
- And goto url http://localhost:3000
- To test edit text in server.js , save and reload web to check hot-reload work
Stopping all the running containers is also simple with a single command:
docker-compose down
With custom docker-compose
docker-compose -f docker-compose.dev.yml down
If you need to stop and remove all containers, networks, and all images used by any service in docker-compose.yml file, use the command:
docker-compose down --rmi all