DEV Community

Cover image for Build a REST API with Node, Express & MongoDB!

Build a REST API with Node, Express & MongoDB!

Bennett Dungan on August 01, 2019

In this project we will be creating a way for people to subscribe, update, retrieve and delete data on Youtube accounts. We’ll do this by coding a ...
Collapse
 
mnepita profile image
Martin Nepita

Very interesting reading, this is really helping me out understand how routing works in expressJS.

thank you so much

Collapse
 
waqar903 profile image
waqar903

Nice and Good Explaination..!
Can you please tell me what next? i would like to upload this Api to website for-example: example.com/api/posts instead of localhost:3000/posts
how can i do that?
Thanks.

Collapse
 
abidtkg profile image
Abid Hasan

get some knowledge about deployments.

Collapse
 
ourystd profile image
Oury Diallo

Wow just great
I'd like to have a mantor like you !

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao

Congrats on your first long-form article :)

Collapse
 
beznet profile image
Bennett Dungan

thank you! Took me much long than anticipated haha

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao

It's alright I always take a much longer time to write technical articles.

Collapse
 
coolrocks profile image
Info Comment hidden by post author - thread only accessible via permalink
Anant Rungta • Edited

I have a Youtube Channel, in which I discuss tutorials, and react.js projects SEO, SSR.
Please check it out and maybe we can collaborate on something.

Video : youtube.com/watch?v=66owdRkc8pI
Channel : youtube.com/c/CodersLifeSimplified

Collapse
 
jeremy254 profile image
jeremywambugu

This is a very important article since it shows the process from beginning to end so even if you had issues with some topics in the middle it ironed them out. Good job mate! I really appreciate it

Collapse
 
vicentenoriega profile image
Vicente Noriega

How can I solve a Corss origin?, I'm calling localhost:3000/subscribers/ endpoint through react application which is running on localhost:3001

Collapse
 
miguelti profile image
Miguel Alemán

Hi, Vicente ! You can solve cross origin setting a header in your server definition.

res.header("Access-Control-Allow-Origin", "YOUR-DOMAIN");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); 
Enter fullscreen mode Exit fullscreen mode

But if you want you can delegate that to a lib called "cors" that you can install: github.com/expressjs/cors#installa...

And that you can use simply by calling it as a Middleware.

var express = require('express')
var cors = require('cors')
var app = express()

app.use(cors())
Enter fullscreen mode Exit fullscreen mode

It is important to remember that allowing cors without specifying a known domain could be dangerous since anyone would be able to reach your api, be careful and if you have any questions don't hesitate to ask :)

saludos, paisano !

Collapse
 
vicentenoriega profile image
Vicente Noriega

Hey thanks man for some reason I was missing put my domain there, but now it's work fine.

Gracias por el support carnal =).

Collapse
 
armanahmedjony profile image
Arman Ahemd

thanks brother for showing this.

Collapse
 
thembastacks profile image
Themba-Stacks • Edited

I've been reading on routing the whole evening, this article just shed a lot of light, thank you.

Collapse
 
beznet profile image
Bennett Dungan

So happy to hear it helped! :)

Collapse
 
l4znet profile image
Charly Escalona

Thank for this tuto ! It will really useful for me :)
And happy new year

Collapse
 
olsard profile image
olsard

Awesome, great explained. Thanks a lot!

Collapse
 
peterpan profile image
peterpan

Thank you very much.This is the easy-to-follow blogs for beginner like me.I really enjoy it.Keep doing ! :)

Collapse
 
ryzhov profile image
Aleksandr N. Ryzhov

Thanks for article, I was test your code it really work great!

Collapse
 
beznet profile image
Bennett Dungan

Awesome! Glad it was useful :)

Collapse
 
joebien profile image
joebien

I think app.listen(3004, () => console.log('server started')) should be app.listen(3000, () => console.log('server started'))

Collapse
 
dhanibaharzah profile image
dhanibaharzah

thank you so much Bennett, your explanations are working !!

Collapse
 
reddy910 profile image
Raja Jaidi

Great Article,
clean code, straight to point, and great explanation. as a beginner helped me a lot.
thanks

Collapse
 
beznet profile image
Bennett Dungan

thank you!

Collapse
 
johndavemanuel profile image
John Dave Manuel

Great tutorial! May I know how or where can I deploy this API?

Collapse
 
travelingwilbur profile image
Wilbur Suero

I love this article, great job!

Found a small error in the catch for the update subscriber, it's missing the (err) parameter part.

Thanks!

Collapse
 
shubham030 profile image
Shubham Gupta

Was a great guide for a beginner
Liked it lot <3

Collapse
 
sumitsharma profile image
SUMIT SHARMA

user == null doesn't work for me, instead I have to use user.length == 0. May be because I am noob.

Collapse
 
didin1453fatih profile image
Didin 👨‍💻

I think you can combine Express JS and dbdesigner.id as your database designer. This can make your project readable and clear documentation.

Collapse
 
saif5017 profile image
Saif5017

dude this has so far been the most useful tutorial i've ever followed

Collapse
 
mgrachev profile image
Grachev Mikhail

Another useful tool - github.com/dotenv-linter/dotenv-li....
It’s a lightning-fast linter for .env files. Written in Rust.

Collapse
 
topsailcashew profile image
Nathaniel Senje • Edited

Not sure if you will get to see this considering how long it has been, but I having a problem with the middleware, for some reason it always returns "User not found", not sure why

Collapse
 
danualbantani profile image
Nya Kitu!!!

Hi, thank you for nice article. And it work great!
Iam new in nodejs and mongodb. How to make data have child?

Like:
name: tommy
subscribedChannel: has multiple here

Thank you

Collapse
 
eziyadah profile image
Eiyad Z.

Thank you for the great article!

Collapse
 
codingtoye profile image
CodingToye

Shame it's not original.

For the original tutorial see -> https://www.youtube.com/watch?v=fgTGADljAeg&ab_channel=WebDevSimplified

Collapse
 
beznet profile image
Bennett Dungan • Edited

This is noted in the intro to the tutorial along with the link the this video.

Some comments have been hidden by the post's author - find out more