DEV Community

Cover image for JavaScript CRUD Rest API using Nodejs, Express, Sequelize, Postgres, Docker and Docker Compose

JavaScript CRUD Rest API using Nodejs, Express, Sequelize, Postgres, Docker and Docker Compose

Francesco Ciulla on February 12, 2023

Let's create a CRUD rest API in JavaScript, using: Node.js Express Sequelize Postgres Docker Docker Compose All the code is available in th...
Collapse
 
antonfridlund profile image
Anton Fridlund

Hello!
I think this read was quite interesting, good guide.

There were however a few things i noticed that I don't often see in modern JavaScript programming.

Usually you would avoid chaining calls with ".then" since it is bad practice, use "await" instead. Is there a reason why you chain your calls like this even when it's bad practice?

Exporting arrow functions in the controller leads to poor readability and is also not a common way to handle controllers. You would probably want to create a class that represents the controller and export that class. Do you have a reason for exporting arrow functions?

Something that I was wondering throughout the guide was why you use "require" instead of "import", basically why commonjs instead of esm? Commonjs is since long dying and i would not recommend using it.

The JavaScript code in the guide seems to be quite old and I would be careful if I were to use it.

Collapse
 
francescoxx profile image
Francesco Ciulla

nice breakdown Anton! this is not optimized and it's meant to get things fast. the focus here was more on creating the endpoints and the docker image and the connection with the db.

about the async await, I just used the suggested code, but in other guides I used the await, honestly I prefer that one

this is not meant to be a production ready code, but it's a way to undesrtand the basics!

Thanks for your feedback, I will probably level up the code in the next guide!

Collapse
 
antonfridlund profile image
Anton Fridlund

Thank you for the explanation!
I was happy to see such a thorough guide as a whole.

It was interesting to see how you containerize the application and I also liked the testing of the end product using Postman and TablePlus, great tools.

Thank you for the guide!

Thread Thread
 
francescoxx profile image
Francesco Ciulla

my idea was to start with this and maybe focus more on the upcoming Typescript example. this article is a breakthrough of something I coded live on youtube.

Thank yuo for your feedback, highly appreciated instead of the usual "asweome" comment! 💙

Thread Thread
 
karlkras profile image
Karl Krasnowsky

A typescript version would be great! Thanks for your effort.
Awesome! 😄

Thread Thread
 
francescoxx profile image
Francesco Ciulla

it's on the todo list. thanks

Collapse
 
edwardfernando profile image
Edward Fernando

Thanks for this tutorial @francescoxx

When I run this on my machine, although the server is started, it is stuck when calling the endpoint. It does not returning the response.

Im currently using mac with m1 chip. Any pointers about this?

Collapse
 
francescoxx profile image
Francesco Ciulla

try docker ps -a. can you see the containers up and running?

Collapse
 
edwardfernando profile image
Edward Fernando • Edited

Oh, this is because i missed the next() function call.

app.use((req, res, next) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
    next();
});
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
francescoxx profile image
Francesco Ciulla

oh I see! glad you fixed it

Collapse
 
edwardfernando profile image
Edward Fernando
➜  ~ docker ps -a
CONTAINER ID   IMAGE               COMMAND                  CREATED        STATUS          PORTS                              NAMES
d3f626718969   efs/node_live_app   "docker-entrypoint.s…"   15 hours ago   Up 39 seconds   3000/tcp, 0.0.0.0:3333->3333/tcp   node_app
3c423213aeff   postgres:12         "docker-entrypoint.s…"   15 hours ago   Up 15 hours     0.0.0.0:5432->5432/tcp             node_db
Enter fullscreen mode Exit fullscreen mode

It is actually running

Thread Thread
 
edwardfernando profile image
Edward Fernando

in the index.js, i tried to commented out these lines and it is working now:

// app.use((req, res, next) => {
//     res.setHeader('Access-Control-Allow-Origin', '*');
//     res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
// });
Enter fullscreen mode Exit fullscreen mode

I still don't understand what happened. Any pointers / explanation would be appreciated much.

Collapse
 
sikehish_ profile image
Hisham Akmal

Well written and documented. Thanks a ton! But i've got one doubt regarding docker compose and docker build. Arent we supposed to build the image first and name it node_live_app using the docker build command and then execute docker compose build?

Collapse
 
francescoxx profile image
Francesco Ciulla

no, docker compose build just builds the image. you can even just run docker compose up --build , or docker compose up.

I just did it to show the step-by-step commands to make it clearer

Collapse
 
sikehish_ profile image
Hisham Akmal

Oh okay. Thank you!

Thread Thread
 
francescoxx profile image
Francesco Ciulla

you are welcome

Collapse
 
shoban12 profile image
Mude Shoban Babu

Thanks so much for detailed course. Just a correction in link,

The youtube link that you have kept in the bottom of the course content takes to "Build a CRUD Rest API with Kotlin, Postgres, Docker and docker compose" instead of "JavaScript CRUD Rest API using Nodejs, Express, Sequelize, Postgres, Docker and Docker Compose", it should be updated to https://www.youtube.com/watch?v=Uv-jMWV29rU&t=236s

Collapse
 
francescoxx profile image
Francesco Ciulla

fixed, thanks!

Collapse
 
shoban12 profile image
Mude Shoban Babu

Thank you Francesco.

Thread Thread
 
francescoxx profile image
Francesco Ciulla

you are welcome Mude

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great post!

Collapse
 
francescoxx profile image
Francesco Ciulla

thanks Bobby!

Collapse
 
charlesr1971 profile image
Charles Robertson

This is a first class tutorial. Very clear and precise.
I’ve been a server side Dev for nearly 20 years, using a language called Coldfusion. In Coldfusion we have a tag called CFQUERY, which we can write SQL, inside.
So, for example:

<cfquery name="getEmployees" datasource="cfdocexamples">
 SELECT FIRSTNAME,LASTNAME,EMAIL,PHONE
 FROM EMPLOYEES
</cfquery>
Enter fullscreen mode Exit fullscreen mode

Obviously, you can write extremely complex SQL queries here.
I have only been using NodeJs for a few years now and have always wondered how easy it is to increase the complexity of the SQL query in a NodeJS Rest API application?

CRUD is all very well and is great for highlighting the basics, but, in the real world, business requirements might warrant more complexity?

Collapse
 
charlesr1971 profile image
Charles Robertson

Hi Francesco

I have set up a codesandbox VM, to try and emulate this tutorial.

I see that your table is called:

Users
Enter fullscreen mode Exit fullscreen mode

But you connect via:

const User = define("user", {
  id: {
    type: INTEGER,
    autoIncrement: true,
    allowNull: false,
    primaryKey: true,
  },
  name: STRING,
  email: STRING,
});
Enter fullscreen mode Exit fullscreen mode

How does your code find the Users table?

Collapse
 
kaybeckmann profile image
KayBeckmann

I am new at coding. Will try it on the weekend. Thank you for this tutorial.

Collapse
 
francescoxx profile image
Francesco Ciulla

please let me know if it worked. thank you

Collapse
 
kaybeckmann profile image
KayBeckmann

Finaly I tested it today.
It worked very well.
a little spelling mistakes in "docker-compose" for starting the second container, but this was easy to fix.
now I can make a little backend for testing in local network :D
Thank you so much.

Thread Thread
 
francescoxx profile image
Francesco Ciulla

amazing, glad it helped. do you mean a spelling mistake on your end or in the tutorial?

Collapse
 
cavo789 profile image
Christophe Avonture

A few weeks ago, I've discover postgrest, just crazy easy. postgrest.org/en/stable/index.html

It's also open data, directly query your postgresql db just using URIs.

Collapse
 
toul_codes profile image
Toul

Nice write up! If I ever use this stack I’ll be sure to keep this article nearby.

Collapse
 
francescoxx profile image
Francesco Ciulla

thank you Toul!

Collapse
 
ebuka1anthony profile image
ebuka anthony

nice article, i learnt a lot, u keep inspiring me fran

Collapse
 
francescoxx profile image
Francesco Ciulla

thank you Ebuka!

Collapse
 
ebuka1anthony profile image
ebuka anthony

you're welcome, now let me use your tutorial article & practicccccccccccccce

Thread Thread
 
francescoxx profile image
Francesco Ciulla

good luccccccck

Collapse
 
farhanacsebd profile image
Farhana Binte Hasan

Will use this one day. Thanks for sharing and making a detailed article.

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome

Collapse
 
odeyale2016 profile image
Odeyale Kehinde Musiliudeen

Welldone Boss

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome

Collapse
 
fu91t1v3 profile image
⚡Abiola⚡

This is beautiful. Never tried Postgres with Node and Express, before, but this is surely gonna help. Thanks.

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome!

Collapse
 
jakeroid profile image
Ivan Karabadzhak

Nice post 👍

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome

Collapse
 
lbino97 profile image
lbino97

Amei o post

Collapse
 
francescoxx profile image
Francesco Ciulla

🔥

Collapse
 
ashishbeetle profile image
Ashish Binu

Awesome ! <3

Collapse
 
francescoxx profile image
Francesco Ciulla

thank you

Collapse
 
nerajno profile image
Nerando Johnson

Time to use this for an idea I have....

Collapse
 
francescoxx profile image
Francesco Ciulla

good luck

Collapse
 
clericcoder profile image
Abdulsalaam Noibi

WOW,What a great knowledge you have shared Francesco.

Collapse
 
francescoxx profile image
Francesco Ciulla

thank you @noibisdev

Collapse
 
lotfijb profile image
Lotfi Jebali

Thanks for sharing, well explained and documented 😁

Collapse
 
francescoxx profile image
Francesco Ciulla

thank you @Lotfij

Collapse
 
codeofrelevancy profile image
Code of Relevancy

A great resource for anyone looking to build a CRUD API..

Collapse
 
francescoxx profile image
Francesco Ciulla

thank you @codeofrelevancy !!

Collapse
 
nikofox profile image
sborowski

Great post. It's been long since I last used express and sequelize.
Too bad there's no validation for incoming data.

Collapse
 
francescoxx profile image
Francesco Ciulla

indeed. there are probably other ways to validate input tho and I should double check

Collapse
 
traleeee profile image
Tra Le

Something missin... hmm, maybe that's typescript i guess

Collapse
 
francescoxx profile image
Francesco Ciulla

ye probably

Collapse
 
pradumnasaraf profile image
Pradumna Saraf

Great blog

Collapse
 
francescoxx profile image
Francesco Ciulla

Pradumna the best!

Collapse
 
yandev profile image
Yan

Thanks a lot !

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome

Collapse
 
rafaelcerqueira profile image
Rafael Freitas

Amazing! Thank you for the post.

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome Rafael

Collapse
 
alexsmith12 profile image
Alex smith

fantastic post Francesco, what excellent details you have offered.
I'm grateful.

Collapse
 
francescoxx profile image
Francesco Ciulla

you are welcome Alex!

Collapse
 
dlmarroco profile image
Danilo Marroco

Great post, thanks for sharing!

Collapse
 
francescoxx profile image
Francesco Ciulla

Thank you Danilo, highly appreciated!

Collapse
 
spaniard95 profile image
Panagiotis Spanos

Hello Francesco,
i really enjoyed the tutorial but i am stuck in the tableplus step. When i connect with the node_live_db its not empty

Image description
Any ideas why that happened?
Image description

Collapse
 
francescoxx profile image
Francesco Ciulla

interesting, can you try to remove the volume and try again? of course be careful if you are ona production environment!!

Collapse
 
spaniard95 profile image
Panagiotis Spanos • Edited

Still the same, i first stopped the container and deleted the volume and recreated it and second try i stopped the container and i changed the docker-compose.yml file volumes part with
volumes:
# node_db_data: {} # remove the old volume
data: {} # create a new volume

to maybe create a new db folder, but both tries it was not empty.

I also installed psql to check the db and i get that "i did not find any relations" when i typed \dt

Thread Thread
 
francescoxx profile image
Francesco Ciulla

try to copy past my docker compose and try agaqin