It's a basic CRUD REST API app, built on top of NodeJs. Basically, the main theme of this project is helping beginners to learn how to build REST API with CRUD functionalities, how to deploy and test the app.
Click to see the Table of Content
This project is built using the following tech stack:
- NodeJS
- Express
- Postgresql
- nodejs ^12.0.0
- dotenv ^9.0.2
- express ^4.17.1
- pg ^8.6.0
Fork this repo and then clone that repo into your local machine.
Please, goto this link, download and install Postgresql into your OS. Then open your terminal and run these commands.
CREATE ROLE me WITH LOGIN PASSWORD ‘password’;
ALTER ROLE me CREATEDB;
-d postgres -U me
CREATE DATABASE crudapi;
- Create a file named
.env
- Copy codes from
.env.example
file - Paste it in
.env
file - By default all the variables are assigned a value. Keep it as it is.
- But if you create Database and users with different name, password (as not same as these instructions), you can change these variables as expected
npm install
npm start
For testing the CRUD functionalities, you have to install curl
in your OS.
To check if your OS has curl pre-installed, open the terminal/cmd and run the following command:
curl --version
If curl is pre-installed, it will show the version of the curl here. Otherwise, it'll say, 'curl' is not recognized as an internal or external command
.
If curl is not installed, then please follow the top answers given to this question in stackoverflow.
And you can also read this article.
Open the terminal. Now run the following commands:
curl -X POST -d "name=me&email=me@example.com" http://localhost:5000/users
This command will create a user with these infos:
name: me
email: me@example.com
If this command has been successfully executed, then it will return the id of the new user as the response.
User's created with id: 11 // This number can be different for you
curl -X GET http://localhost:5000/users
This will read all the users from the database and show them like:
[{"id":1,"name":"admin","email":"admin@example.com"},{"id":11,"name":"me","email":"me@example.com"}]
If no user exists, it will show a empty array []
.
You can also get the info of one specific user. Run this command.
curl -X GET http://localhost:5000/users/1
If the user with id 1 is available, it will show you the info of that user like:
[{"id":1,"name":"admin","email":"admin@example.com"}]
curl -X PUT -d "name=Iam&email=iam@example.com" http://localhost:5000/users/11
This command will update info of the user having id: 11
with these information:
name: Iam
email: iam@example.com
If this command has been successfully executed, then it will return the id of the new user as the response.
Updated user with id 11
You can check the result by running GET request and see the info whether it gets changed or not.
curl -X DELETE http://localhost:5000/users/11
This command will delete the user having id: 11
from the database. And return the id of the user as response like:
Deleted user with id 11
You can check the result by running GET request also.
Note: In this command, if you are using a different port number instead of 5000, replace
5000
with your port number inhttp://localhost:5000/users
Keep all the information related to the package. For example:
-
Package name
-
Package Description
-
Package Version
-
Entry Point (file, ex: index.js) of the package
-
Scripts
-
Dependencies
-
Dev Environment Dependencies
This file is the entry point of the app. That means if any user hit the domain address of your app, the request will be sent to this file and then further process will happen. In this project, this file is executing the following tasks:
-
Intialize the app
-
Start the server
-
Listen all the http requests to different api endpoint (domain path, ex:
/users
or/users/:id
) -
Connects the http requests with database and prepare necessary responses
-
Finally, send the responses to the users
This file keeps all the routes available in this app and index.js
reads routes/path/api-endpoints from this file.
This file creates the connection between the database and the app. We have to use this pool to run any kind of query into the database. It does the following tasks:
-
Read the environment variables that are necessary
-
Create connection pool using these variables
-
Enable SSL connection for production
This file keeps all the queries that are needed to be run before starting the server. These queries are used for:
-
Creating necessary tables
-
Creating basic users
These queries are needed to prevent errors while executing requests through api-endpoints
This file keeps all the queries based on api endpoints. It does following tasks:
-
It exposes some processing functions
-
These functions receive the request sent by the users
-
Based on the different endpoints, different function runs different queries to the database and builds the response object
-
Returns these responses
index.js
uses these functions to process the users' requests
This file keeps an example of .env
. We have to create .env
files with the help of this file.
-
Shows which environment variables are needed
-
What values these variables are expecting
This file is used to tell Heroku
what scripts should be run to start the server.
This file is also used for Heroku
. It's just giving a short detail of the app to Heroku.
-
☝️ ☝️ ☝️ Click on the button above
-
Then type a name in the app name field
-
Then connect to github
-
And deploy
-
When the deployment will end, click on the
View App
button
-
GoTo
Resource Tab
in the dashboard -
Here, in the Add on search field, type
Heroku Postgres
-
Then from the suggestion click on the
Heroku Postgres
-
From the popup, select
Hobby Dev - Free
as plan and click Provision
The database will be created.
-
At first, Goto the settings tab
-
Then click on
Reveal Config Vars
from the below -
Search for
DATABASE_URL
this variable. If it's here, you don't need to follow the following steps. Otherwise follow. -
Goto the resource tab again and select
Heroku Postgres
-
Then, select the
Settings
tab -
Select
View Credentials
button -
Copy the value of
URI
field -
Go back to the dashboard of the app and goto the settings tab
-
Then click on
Reveal Config Vars
from the below -
Type
DATABASE_URL
in the KEY input field -
Paste the value of
URI
you copied in the VALUE input field
For more details, read this 👉 article.
Note: While testing the app in Heroku (after deployment), follow the same instructions given How to test the app section and replace
http://localhost:5000
this with the domain given by Heroku for your app likehttps://<Domain given by Heroku>/users
.
If you find any typo or think of any better or simple project structure, then create an issue and describe your thought here. I will discuss that with you. And then you can give pull request with your implementations.