RESTful-APIs in Node, Express and MongoDB
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


A Todo application to demostrate RESTful-APIs.
Developed in Node.js, Express.js and MongoDB.

User can signup and login the application. After login, they can peform CRUD operations.

To save data in the MongoDB, Mongoose an ORM is used to create Schemas. When a user saves a new doc in the MongoDB then an instance of the Schema is created. Below is the example of ToDo Schema create in Mongoose.

const mongoose = require('mongoose');

const Todo = mongoose.model('Todo', {
  text: {
    type: String,
    required: true,
    trim: true
  completed: {
    type: Boolean,
    default: false
  completedAt: {
    type: Number,
    default: null


module.exports = {

JsonWebToken is used for authentication.

BCrypt is used for password encryption.

Once a user is sign up then they will get back a token from JWT.
Below is the code for creating that token:

UserSchema.methods.generateAuthToken = function() {
	const user = this;
	const access = 'auth';
	const token = jwt.sign({
		_id: user._id.toHexString(),
	}, 'secret').toString();

	user.tokens = user.tokens.concat([{ access, token }]);

	return => {
		return token;


When the user, login then the plain password is hashed and compared with the password in the MongoDB.
Below is the code for that:

UserSchema.statics.findByCredentials = function(email, password) {
	const User = this;

	return User.findOne({ email })
	  .then((user) => {
			if (!user) {
				return Promise.reject();

			return new Promise((resolve, reject) => {, user.password, (err, res) => {
					if (res) {
					} else {