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...
For further actions, you may consider blocking this person and/or reporting abuse
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.
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.
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
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.
We can use Postman app to send request form get/post. You will receive response data.
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
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?
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
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
Oh my Gosh, I overlooked that.
Thanks, Sam. I'll put that into consideration in my next post.
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.
Sorry @Natalia, The article has been updated to include Morgan.
Howdy.
Great job on the article! I thought I would make a few suggestions just in case they haven't been made:
err
, thenerror
, thenerr
again). Keeping parameter names consistent and sticking to one particular pattern can help with readability and is in general a good practice.if
/else
in one route handler, consider using it in all. If you would rather stick toif
/return
patterns, do that instead. Just stay consistent!console.log
after usingapp.listen
could have been passed as a callback.const
andlet
would have both come in handy all throughout this project.express.static
directory you have already prepared it to serve that file on the root route.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!
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
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?
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 checkConfig.js
is in the right case? I look forward to hearing from you :)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.
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!!
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
Nice article, so awesome and simple to follow
Thanks for reading, Thoby
Welcome Sam! Was enlightening
Hi Samuel, Thanks for sharing this. Can I have some deep explanation about bundle.js code.
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?
Thank you for this. It's now updated with the link to Part 2
When I refresh the page in a page that is directed to the main page is there a solution to this problem