Introduction

Before we start to code let us first understand a few things.

What is MEAN?

So, basically MEAN stands for MongoDB, Express, AngularJS, and NodeJS.
Now the question is, what are these?
Now we will use these amazing full-stack Javascript frameworks to build a Todo Web Application.
Let us now see which version we will be using for this tutorial.
Instruction
Step 1
Let’s start first by installing Nodejs.
To build our project we need to install Nodejs. Let’s see how to do that below.
Type the below command in the terminal to install nodejs.
Once the installation process is done we can check our nodejs version using the below command.

$ node –v (display the node version)

We can also check our npm (Node Package Manager) version using the below command.

$ npm –v (display the npm version)

Node Package Manager is basically used to build projects and to download nodejs library.
Once done, we can now move to Step 2.
Step 2
Installing MongoDB.
To install MongoDB we need to run the below command.
  1. $ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
  2. $ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
  3. $ sudo apt-get update
  4. $ sudo apt-get install -y mongodb-org
Once we install our MongoDB, now we are ready for Step 3.
Step 3
Building our project. To build using the below command
Step 4
Installing Express. We can install express using the below command.

$ npm install express –save

and this will install the express and also add the package in the pacakage.json folder.
Our Complete Project Structure
Step 5
Starting our Nodejs Server.
Lets first create server.js file inside todo folder
todo/server.js
  1. //calling express library
  2. var express = require('express');
  3. var app = express();
  4. //GET request
  5. app.get('/', function(req, res){
  6. res.send('<h1>Welcome to Todo Web Application!!');
  7. });
  8. app.listen('8080', function(){
  9. console.log('Server Running!!');
  10. });
Now let’s start our server using the below command.
$ nodes server.js
Wooo!! Server Running :)
Now to access our application we need to follow the guide shown in the below image.
Now we will build our application frontend using Angularjs.
Step 6 - Creating Our Frontend
Before we build our front end let's create a folder named [app] inside the todo folder where we will store our frontend files and then we will see how to connect our fronted with the server.
Now let’s start building our frontend using Angularjs.
todo/app/index.html
  1. <!doctype html>
  2. <html lang="en" ng-app="todoApp">
  3. <head>
  4. <!-- Required meta tags -->
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  7. <!-- Bootstrap CSS -->
  8. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  9. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  10. <script type="text/javascript" src="app/app.js"></script>
  11. <title>Todo App | MEANjs</title>
  12. <style type="text/css">
  13. .bg-hackclub{
  14. background: #F34C5E;
  15. color: #fff;
  16. }
  17. </style>
  18. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" type="text/css" />
  19. </head>
  20. <body>
  21. <div class="jumbotron jumbotron-fluid bg-hackclub">
  22. <div class="container">
  23. <h1 class="display-4">Todo App</h1>
  24. <p class="lead">This is MEANjs built in web application for Hack Club.</p>
  25. </div>
  26. </div>
  27. <div class="container">
  28. <div class="row" ng-controller="createTodoController">
  29. <div class="col-md-6 offset-md-2">
  30. <div class="input-group input-group-lg">
  31. <input type="text" ng-model="todo.task" class="form-control" placeholder="Write your daily task here....." aria-label="Large" aria-describedby="inputGroup-sizing-sm">
  32. </div>
  33. </div>
  34. <div class="col-sm-3">
  35. <button type="button" ng-click="createTodo()" class="btn btn-outline-info btn-lg" >Add Task</button>
  36. </div>
  37. </div>
  38. <hr>
  39. <div ng-controller="myCtrl">
  40. <div class="row" ng-repeat="task in tasks.data">
  41. <div class="col-md-7 offset-md-2">
  42. <div class="input-group mb-3">
  43. <div class="input-group-prepend">
  44. <div class="input-group-text">
  45. <input type="checkbox" aria-label="Checkbox for following text input">
  46. </div>
  47. </div>
  48. <h1 class="form-control">{{task.task}}</h1>
  49. <div class="input-group-append">
  50. <div class="input-group-text bg-danger" ng-click="deleteTask(task._id)">
  51. <i class="fa fa-trash-o text-white"></i>
  52. </div>
  53. </div>
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. </div>
  60. </div>
  61. <!-- Optional JavaScript -->
  62. <!-- jQuery first, then Popper.js, then Bootstrap JS -->
  63. <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  64. <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  65. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  66. </body>
  67. </html>
Create an app.js file to load our angularjs components.
todo/app/app.js
  1. var app = angular.module('todoApp', []);
  2. app.controller('createTodoController', function($scope,$http) {
  3. $scope.createTodo = function(){
  4. //console.log($scope.todo);
  5. $http.post('api/create/todo', $scope.todo)
  6. .then(function(success){
  7. //success callback
  8. console.log($scope.todo);
  9. console.log(success.status);
  10. }, function(error){
  11. //error callback
  12. console.log(error.status);
  13. });
  14. }
  15. });
  16. app.controller("myCtrl", function($scope, $http) {
  17. $http.get("api/get/tasks")
  18. .then(function (tasks) {
  19. $scope.tasks = tasks;
  20. //console.log(tasks);
  21. });
  22. $scope.deleteTask = deleteTask;
  23. function deleteTask(taskId) {
  24. $http.delete("/api/delete/task/"+taskId)
  25. .then(function(){
  26. //success callback
  27. console.log('success');
  28. },function(error){
  29. //error callback
  30. console.log('Error');
  31. });
  32. }
  33. });
Step 7
Let’s create our Database.
For creating a database using MongoDB first we need to run our MongoDB server using the below command
$ mongod
Once we see our server is running now we are required to create database. So quickly move into a new terminal tab and run the below command for creating a database using MongoDB.
Once we are done now we need to connect our MongoDB with our MEANjs application.
Note

Don’t stop the MongoDB server otherwise it won’t get connected with our MEANjs application.
Step 8 - Connecting to MongoDB
To connect first we need to call the mongodb library used by the express.
So let's install the mongoose library
$ npm install mongoose –save
This will install mongoose and will add it to the package.json file.
Step 9
Installing Body-Parser to parse middleware.
Use the below command to install.
$ npm install body-parser --save
Step 10
Working with Backend.
Now we will work with our server.js file.
In this file we will do the following things.
Todo/server.js
  1. //calling express library
  2. var express = require('express');
  3. var app = express();
  4. var bodyparser = require('body-parser');
  5. var mongoose = require('mongoose');
  6. //connecting to MongoDB
  7. mongoose.connect('mongodb://127.0.0.1:27017/todo');
  8. //creating model
  9. var TaskSchema = mongoose.Schema({
  10. task: {type:String}
  11. }, {collection: 'task'});
  12. var TaskModel = mongoose.model("TaskModel", TaskSchema);
  13. //configure app
  14. app.use('/app', express.static(__dirname + '/app')); //use static file
  15. app.use(bodyparser.json()); // for parsing application/json
  16. app.use(bodyparser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
  17. //GET request
  18. app.get('/', function(req, res){
  19. res.sendfile('app/index.html');
  20. });
  21. //POST request to save todo task in database
  22. app.post("/api/create/todo", createTodo);
  23. function createTodo(req, res) {
  24. var todoTask = req.body;
  25. //console.log(todoTask);
  26. //save the todoTask in db
  27. TaskModel
  28. .create(todoTask)
  29. .then(
  30. function (success) {
  31. console.log('Success');
  32. },
  33. function (error) {
  34. console.log('Error');
  35. }
  36. )
  37. res.json(todoTask);
  38. }
  39. //GET all task
  40. app.get("/api/get/tasks", getAllTasks);
  41. function getAllTasks(req, res) {
  42. TaskModel
  43. .find()
  44. .then(
  45. function (tasks) {
  46. res.json(tasks);
  47. },
  48. function (err) {
  49. res.sendStatus(400);
  50. });
  51. }
  52. //DELETE task
  53. app.delete("/api/delete/task/:id", deleteTask);
  54. function deleteTask(req, res) {
  55. var taskId = req.params.id;
  56. //console.log(taskId);
  57. TaskModel
  58. .remove({_id: mongoose.Types.ObjectId(taskId)})
  59. .then(function () {
  60. res.sendStatus(200);
  61. },
  62. function () {
  63. res.sendStatus(400)
  64. });
  65. }
  66. app.listen('8080', function(){
  67. console.log('Server Running!!');
  68. });
Step 11
Now let’s run our Todo MEANjs Application.
To run we know which command we have to use
$ node server.js
Now create tasks, check the completed task, and delete.
Thank You!!