Node Express drizzle-orm boilerplate in ECMAScript Modules(mjs)
To get a local copy up and running, please follow these simple steps.
Here is what you need.
- Node.js (Version: >= 20.x)
- PostgreSQL (Version: >= 16.x)
- npm
-
Setup Node If your Node version does not meet the project's requirements as instructed by the docs, either manually or using a tool like nvm or volta (recommended)
-
Clone the repo
git clone git@github.com:yadav-saurabh/node-express-drizzle.git
-
Go to the project folder
cd node-express-drizzle
-
Install packages with yarn
npm i
-
Set up your
.env
file-
Duplicate
.env.example
to.env
-
Use
openssl rand -base64 32
to generate a key and add it underJWT_SECRET
in the.env
file. -
Configure environment variables in the
.env
file. Replace<db_name>
,<db_user>
,<db_password>
,<db-host>
and<db-port>
with their applicable values# Database Credentials POSTGRESQL_HOST=<db-host> POSTGRESQL_PORT=<db-port> POSTGRESQL_DB_NAME=<db_name> POSTGRESQL_DB_USER=<db_user> POSTGRESQL_DB_PASSWORD=<db_password>
-
-
Database Setup
-
Quick start database using
docker-compose
- Requires Docker and Docker Compose to be installed
- Will start a local Postgres instance
docker-compose up
-
Manual Database setup
-
Download and install postgres in your local (if you don't have it already).
-
connect to postgres prompt
sudo -u postgres psql
-
Create a new user
CREATE USER my_user WITH ENCRYPTED PASSWORD 'user_password';
-
Create your own local db by executing
create database my_database;
-
Create your own local db by executing
grant all privileges on database my_database to my_user;
-
Now extract all the info and add it to your
.env
. The port is configurable and does not have to be 5432.
-
-
If you don't want to create a local DB. Then you can also consider using services like railway.app or render.
-
-
Setting up the migrations
npm run drizzle:migration-run
-
Run the node server
-
In Development
npm run dev
-
In Production
npm start
-
-
Update the system
sudo apt update # Fetches the list of available updates sudo apt upgrade # Installs some updates; does not remove packages sudo apt full-upgrade # Installs updates; may also remove some packages, if needed sudo apt autoremove # Removes any old packages that are no longer needed
-
install pm2 globally
npm install pm2 -g
-
Follow Development setup to get up and running the server in the instance
-
Expose the instance to the network for the node_server_port (Ingress Rules for TCP protocol)
-
allow firewall to accept the network for the port
sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport node_server_port -j ACCEPT sudo netfilter-persistent save
-
use the public ip of the instance to access the node server ex: 111.11.11.11:node_server_port
-
Expose instance to accepts network on port 80 and 443 (Ingress Rules for TCP protocol)
-
allow firewall for port 80 and 443
sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 80 -j ACCEPT sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 443 -j ACCEPT sudo netfilter-persistent save
-
install nginx
sudo apt update sudo apt install nginx
-
Now, you will create a configuration block for the Node server
sudo nano /etc/nginx/sites-available/your_domain
add this to the file /etc/nginx/sites-available/your_domain
server { server_name your_domain www.your_domain; location / { proxy_pass http://localhost:node_server_port; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
-
Create a symlink, which tells Nginx to look for available web applications in the sites-available folder:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
-
Disable the default symlink otherwise, nginx will redirect all requests to the default site. Use the following command to unlink it.
sudo unlink /etc/nginx/sites-enabled/default
-
Restart the Nginx service using the following command.
sudo systemctl restart nginx
-
Install certbot
sudo snap install core; sudo snap refresh core sudo snap install --classic certbot sudo ln -s /snap/bin/certbot /usr/bin/certbot
-
Obtain an SSL Certificate
sudo certbot --nginx -d example.com -d www.example.com
-
Verifying Certbot Auto-Renewal
sudo systemctl status snap.certbot.renew.service
- Test
- CI/CD