/express_mongo_todo_example

Very simple TODO example to illustrate use of Mongo

Primary LanguageJavaScript

MongoDB + express

Demo: converting very basic express app (Todos) from in-memory data storage to MongoDB.

In this case, the commit diff is worth 1000 words.

Notable things:

  • everything to do with "working with data" is kept in the todos.js module. As far as app.js is concerned, the actual data store could be anything. We did have to change from regular return values to callbacks, though.

  • We make some effort to db.close() properly: leaving connections hanging isn't a good practice, and it can leave your app hanging when it should be shutting down, too.

  • I am cheating a little bit by exporting functions in todos.js that depend on the db (which actually takes a moment to connect). Since it's a web app, it's maybe acceptable that it might not serve requests properly for the first few moments, but it's still not 100% correct.

    The 100% correct way would either be a) even more stilted than it is now with the async callbacks, or b) use some method like promises or generators that I didn't want to get into today.

Also: