DEV Community

Build a Todo App with Node.Js, ExpressJs, MongoDB and VueJs – Part 1

Samuel James on February 08, 2018

In this tutorial, we will build the famous todo application with Node.Js using the ExpressJs framework and MongoDB. Did I forget to tell you? The...
Collapse
 
cyberrob profile image
Pek Bun Ong • Edited

Hi Samuel,thanks for the tutorial!

I clone from your repo and run on my mac with node v8.9.4.

After run node server.js, 127.0.0.1:4000 works fine. But running curl command like your:

$ curl -H "Content-Type: application/json" -X POST -d '{"name":"Going Shopping"}' localhost:4000/api/add

the server just hanging there not responding anything:

Note: Unnecessary use of -X or --request, POST is already inferred.

  • Trying 127.0.0.1...
  • TCP_NODELAY set
  • Connected to 127.0.0.1 (127.0.0.1) port 4000 (#0) > POST /api/add HTTP/1.1 > Host: 127.0.0.1:4000 > User-Agent: curl/7.54.0 > Accept: / > Content-Type: application/json > Content-Length: 25 >
  • upload completely sent off: 25 out of 25 bytes

After Ctrl+C, server showed:
node server.js
App listening on port 4000
POST /api/add - - ms - -

Could you help me out here? Thank you.

Collapse
 
_akash_j_ profile image
Akash Jobanputra • Edited

I was facing the same issue, after hours of searching, I found that downgrading the version of mongoose to 4.7.6 works.
I used below command to downgrade the mongoose module, and then the API worked perfectly.

npm install mongoose@4.7.6

Collapse
 
zsofiaboldizsar profile image
Zsofia Boldizsar • Edited

Thanks for sharing this, Akash. I should have looked at the comments section in the first place. I had the same issue, but with your tip, I managed to solve it.

Collapse
 
tvviem profile image
Triệu Vĩnh Viêm

We can use Postman app to send request form get/post. You will receive response data.

Collapse
 
abiodunjames profile image
Samuel James • Edited

Hi Pek,

My bad! I'm just seeing this. I apologize for my late response.
Hope downgrading solves your problem as suggested by @Akash Jobanputra

Collapse
 
unika profile image
unika • Edited

Hi Samuel!

I followed your first part of the tutorial, but my curl request didn't work.
And my app didn't work, but my IDE didn't produce me any mistakes...

UPDATE

You missed the code:

app.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(server is listening on ${port})
})

My app works with it.

UPDATE

This: curl -H "Content-Type: application/json" -X POST -d '{"name":"Going Shopping"}' localhost:4000/api/add

Show me this:

Unexpected token ' in JSON at position 0

Please, could you help me with it?

Collapse
 
abiodunjames profile image
Samuel James • Edited

Hello Unika,

Sorry for my late response. Use this:

curl -X POST -H "Content-Type: application/json" \
-d "{\"name\":\"Going shopping\"}" http://localhost:4000/api/add

Collapse
 
sambenskin profile image
Sam Benskin

Good article, small suggestion if I may. The use of third party libraries like Morgan and so on is good but it would have been better to introduce them later on and just keep it super simple to begin with. Then as we're reading we can see the code build up from the simple to the more complex as you add in each part.

Other than that, a great read, thanks

Collapse
 
abiodunjames profile image
Samuel James

Oh my Gosh, I overlooked that.

Thanks, Sam. I'll put that into consideration in my next post.

Collapse
 
erikiva profile image
Natalia Vidal

Also the first error I got was that morgan could not be found..I've installed it and it's all working now but it isn't on the package.json nor does it say to install it anywhere.

Thread Thread
 
abiodunjames profile image
Samuel James

Sorry @Natalia, The article has been updated to include Morgan.

Collapse
 
hvdrew profile image
Andrew • Edited

Howdy.

Great job on the article! I thought I would make a few suggestions just in case they haven't been made:

  • Try to stay consistent with your error handling, there are different styles written all throughout this first part of the tutorial (you use err, then error, then err again). Keeping parameter names consistent and sticking to one particular pattern can help with readability and is in general a good practice.
  • If you are going to use an if/else in one route handler, consider using it in all. If you would rather stick to if/return patterns, do that instead. Just stay consistent!
  • The console.log after using app.listen could have been passed as a callback.
  • const and let would have both come in handy all throughout this project.
  • The route for the Index.html file is redundant. After setting the express.static directory you have already prepared it to serve that file on the root route.
  • It's probably easier for your readers if you group require statements by their type. For example, group all 3rd party dependencies, then group all file dependencies from your project, then start declaring things like the app instance. This makes it easier to follow for your readers.

Other than that the article was on point, great work. Best of luck to you!

Collapse
 
ashgale profile image
AshGale

Nice article Samuel.
Code seem easy enough to follow and get going to mission accomplished.

I would suggest to add at the start (even if git isn't needed, its good practice)
open the cmd line in your newly created folder and get node started with:

mkdir todo-app && cd todo-app && mkdir app && mkdir public
git init
npm init

Then modify the package.json to add the dependencies:

ps and to correct small typeo for creating the config.js file to Config.js

Collapse
 
ctuxboy profile image
Christophe Hollebeke • Edited

Hello,

It's my first steps with NodeJS. Trying this nice tutorial, but i have this error:

ubuntu@nodejs:~/todo-app$ node server.js
internal/modules/cjs/loader.js:583
throw err;
^

Error: Cannot find module './app/Config'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object. (/home/ubuntu/todo-app/server.js:19:14)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:279:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:696:3)

How can i solve this?

Collapse
 
abiodunjames profile image
Samuel James • Edited

Hi Christopher,
From the debug information, I could see that Config.js could not be imported. Please note that file names are case sensitive on Unix machines. Could you please double check Config.js is in the right case? I look forward to hearing from you :)

Collapse
 
ctuxboy profile image
Christophe Hollebeke

Hi Samuel,
Thanks a lot for your helpful answer. Yes that was the problem. Shame on me :-/

Ok, after that start the server: '$node server.js' but shows an error:

(node:2977) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

After searching the net, found a solution. I'm adding this line to server.js:
const dbconfig = { useNewUrlParser: true, };

And add the variable in this line:
mongoose.connect(config.DB, dbconfig)

Now it works :-)

I'm a webdesigner buildign WP websites, but i want to learn NodeJS. Having basic JS knowledge, so i'm an absolute NodeJS beginner.
Trying learning a lot with this useful and awesome examples.
And... sorry for my poor english.

Collapse
 
georgiagallant333 profile image
georgiagallant333

Hi Samuel! Thanks so much for this article. I am running into an issue where when I rune node server.js it says:

TypeError: Router.use() requires callback function but got a [object Object]
at /Users/georgia/todo-app/node_modules/express/lib/router/index.js:423:13
at Array.forEach ()
at Function.use (/Users/georgia/todo-app/node_modules/express/lib/router/index.js:419:13)
at Function.use (/Users/georgia/todo-app/node_modules/express/lib/application.js:193:14)
at Object. (/Users/georgia/todo-app/server.js:29:5)
at Module._compile (internal/modules/cjs/loader.js:721:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)

And I'm not sure what is the problem. I was wondering if you have any thoughts? Thank you!!

Collapse
 
codehimanshu profile image
Himanshu Agrawal

The packages used are way too old, and doesnt support Promises. After updating the packages to :
express 4.17.1
method-override 3.0.0
mongoose 5.6.0
Everything got working

Collapse
 
thobyv profile image
Thoby V ijishakin

Nice article, so awesome and simple to follow

Collapse
 
abiodunjames profile image
Samuel James

Thanks for reading, Thoby

Collapse
 
thobyv profile image
Thoby V ijishakin

Welcome Sam! Was enlightening

Collapse
 
iamsanthi_naidu profile image
Santhi Lakshmi Yenumula

Hi Samuel, Thanks for sharing this. Can I have some deep explanation about bundle.js code.

Collapse
 
markjohnson303 profile image
Mark Johnson 👔

This is really awesome, thanks for putting it together! On to part 2 :) Maybe you could edit this post with a link to the second part?

Collapse
 
abiodunjames profile image
Samuel James

Thank you for this. It's now updated with the link to Part 2

Collapse
 
a7medmasry profile image
A7medmasry

When I refresh the page in a page that is directed to the main page is there a solution to this problem