Sample calorie counter app, Node.js backend and Single Page Application frontend using Bootstrap and Knockout. The application was developed for learning purposes and to show how to use MongoDB in a Node.js backend, via Mongoose, and how to design a clear and predictable RESTful API.
Front End: (see client folder)
Back End: (see server folder)
Database:
Yeoman generator used for Front End: https://github.com/leszekhanusz/generator-knockout-bootstrap/tree/master/app/templates
- Clone git repository.
- Install MongoDB, and use the mongo shell to run
use calorie-tally-db
so a new database is created. The connection URI used from the app is:mongodb://localhost:27017/calorie-tally-db
. - On the server folder, run
node server.js
(you can use nodemon as well), to start the REST API. The API can be accesed on http://localhost:8080/api.
- Install all dependencies using
npm install
andbower install
. - Use
grunt
to build app, andgrunt serve
to serve it and monitor changes at http://localhost:9000.
- Install mocha:
npm install -g mocha
. - Use mongo shell to run
use calorie-tally-test-db
, to create a database for testing purposes. - Execute run_tests.sh, it will spawn a separate instance of the application under a different port, and using the test database, and then, it runs the
mocha
command, which will run all files under test/*.js.
-
Hashed passwords are stored on the DB, never in plain text. I am using bcrypt to hash the password with a salt.
-
A JSON Web Token authentication mechanism was implemented. The user then presents his/her credentials (user name and password), so the backend validates this information with the credentials stored on the DB. If this step is successful, a token is generated, and returned to the user, who will securely store it and use it on every request perform, by passing the token on the header of the HTTP request.
-
Regarding authorization, it is worth mentioning two things:
- a) Some routes are only allowed for admin users, such as
GET /users
(to get all users information),GET /meals
(to get all meals) andDELETE /users
, to remove users. - b) On other routes, mainly the ones that update and create new content, if the user is not admin, it will only be allowed to create/update his own records.
- a) Some routes are only allowed for admin users, such as