In this tutorial, we will learn how to create TODO App using Node.js, mongoose, express and angular.js.
Prerequisites
- Node.js
- MongoDB
- Express
Before creating TODO App using Node.js,mongoose(MongoDB Module for Node),express and angular.js we need to install Node.js, express and MongoDB in our system.We can download and install these software by following link :-
Node.Js :- Install Node.Js
MongoDB :- Install MongoDB
Express :- Install Express
Once you installed software in your system, you can check its installed or not by following command :-
Check Node.Js installation :-
1 |
node --version |
Check MongoDB installation :-
1 |
mongo --version |
Check express installation :-
1 |
express --version |
Setting Up Our Application
Now we will create project folder and we will do setup for node.js TODO application.
1 2 |
mkdir todo-app cd todo-app |
Now we will create package.json file and we will define all the dependency of our node application.
package.json
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "name": "todo-app", "version": "0.0.1", "private": true, "scripts": { "start": "node app.js" }, "dependencies": { "express": "3.5.1", "mongoose": "3.8.8" } } |
Here we are going to use express and mongoose package so we defined in dependencies.
Now we will run the following command to install all packages through npm.
1 |
npm install |
After running this command node_modules folder is created in out project directory.we can check all installed model by running this command in terminal also :-
1 |
npm ls |
Folder Structure of TODO App
config.json [Define All configuration here]
We will define all configuration of project in config.json file.
1 2 3 4 |
{ "mongodbUrl": "mongodb://localhost/todo", "appPort": 6633 } |
Here dbPath is the path of mongoDB for connection and todo is our database name.
port is used for running the Node.js application on this particular port number.We can change these setup anytime according to our requirement.
Application Setup [app.js]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/** * Module dependencies. */ var express = require('express'); var routes = require('./routes'); var http = require('http'); var path = require('path'); var config = require('./config'); var db = require('./lib/db.js'); var app = express(); // all environments app.set('port', config.appPort); app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'html'); app.use(express.logger('dev')); app.use(express.json()); app.use(express.urlencoded()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(path.join(__dirname, 'public'))); // development only if ('development' == app.get('env')) { app.use(express.errorHandler()); } db.connect(config, function(err) { if (err) { throw err; } app.get('/api/todos', require('./routes/findTodo')); app.post('/api/todos', require('./routes/addTodo')); app.delete('/api/todos/:todo_id', require('./routes/deleteTodo')); app.get('/', routes.index); http.createServer(app).listen(app.get('port'), function() { console.log('Express server listening on port ' + app.get('port')); }); }); |
here we define the application setup of express application, which will run on port number 6633 as we defined in config.json file.We have also defined the routes for this application in this file only.