Skip to content

Commit 32fc324

Browse files
committed
Initial Commit
0 parents  commit 32fc324

File tree

7 files changed

+4076
-0
lines changed

7 files changed

+4076
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<h2>NodeJsWithMongoDb</h2>
2+
<h3>Scope</h3>
3+
<p>This tutorials explains how to create REST api using express js, node js & mongodb.
4+
After this tutorial you will be able to create basic get and post REST api services using
5+
node & mongo.<br/>
6+
Put, Patch method will be updated soon in the same tutorials</p>
7+
8+
<h3>What is gulp?</h3>
9+
<p>Gulp is basically a watcher. If you perform any change to your application, then you
10+
have to stop and restart service again and again. Gulp takes this reponsibility for you.
11+
It watches if there is some changes in your application and rebuild it automatically.
12+
You can review gulp settings in gulpfile.js</p>
13+
14+
<h3>What is bookModel.js?</h3>
15+
<p>This file is a structure of your mongodb document. It should be same as mongodb document structure. You can relate it with entity framework in .Net.</p>
16+
17+
<h3>How to run?</h3>
18+
<p>Download repository or clone it using git bash. Navigate to folder in command prompt(windows).
19+
Then run "npm install" to download dependencies. After that "node app.js".
20+
Congrats...your service is active now.</p>
21+
22+
<h2>Happy to server you....Thanks</h2>

app.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**This is a basic tutorial to explain how we can make get and post services using
2+
* express js for node and mongo. mongo is a document based (nosql) database, where
3+
* your records(called document) are saved in form of bson(binary json). Like SQL server &
4+
* oracle and other rdbms it does not allow you to make relations so it is simpler and faster.
5+
* by default mongo runs on port no 27017(though this can be changed form system to system).
6+
* To test your get/post method os services you can use postman, fiddler and many other
7+
* free tools are availaible.
8+
*
9+
* Author : Martand Singh
10+
* Date: 01 June 2018
11+
* Scope : Node Rest Api using Express Js and mongodb as backend.
12+
13+
*/
14+
15+
const express = require('express');
16+
const app = new express();
17+
18+
//mongoose is used for mongo connection
19+
const mongoose = require('mongoose');
20+
21+
//this is the model of your book, which should be same as mongo document.
22+
const Book = require('./models/bookModel');
23+
24+
//mongodb connection. admin is the name of database
25+
const db = mongoose.connect('mongodb://localhost/admin');
26+
27+
//body parse basically read your request and if there is some json then it puts in your request body
28+
const bodyParser = require('body-parser');
29+
30+
// if you have set an environment the it will take port no already set in env otherwise 3000.
31+
//in our case we have set enviornment in gulpfile.js where port no is 8000. So our application
32+
//will work on 8000 port.
33+
const port = process.env.PORT || 3000;
34+
35+
//here we are creating a router for book.
36+
var bookRouter = express.Router();
37+
38+
app.use(bodyParser.urlencoded({extended:true}));
39+
app.use(bodyParser.json()) //we will use json body parser
40+
41+
///books route created. book
42+
bookRouter.route('/books') //books is basically your collection name in mongodb
43+
//defining post request
44+
.post(function(req, res){
45+
var _book = new Book(req.body);
46+
_book.save();
47+
console.log(_book);
48+
res.status(201).send(_book); //201 status for created
49+
})
50+
//defining get request
51+
.get(function(req, res){
52+
var query = {};
53+
54+
//here we are checking if query string name is genre then only we will filter the data.
55+
if(req.query.genre){
56+
query.genre = req.query.genre;
57+
}
58+
//here we are checking if query string name is author then only we will filter the data.
59+
if(req.query.author){
60+
query.author = req.query.author;
61+
}
62+
63+
Book.find(query, function(err, books){
64+
if(err){
65+
res.status(500).send(err); //here we are sending response status code also. as 500 means internal server error.
66+
}
67+
else{
68+
res.json(books);
69+
}
70+
});
71+
});
72+
73+
// here we are defining a route to find a book with its it.
74+
//e.g: http://localhost:8000/api/books/5b10402e453bd55322440e89
75+
bookRouter.route('/books/:bookid')
76+
.get(function(req, res){
77+
78+
Book.findById(req.params.bookid, function(err, book){
79+
if(err){
80+
res.status(500).send(err);
81+
}
82+
else{
83+
res.json(book);
84+
}
85+
});
86+
});
87+
88+
89+
90+
app.use('/api', bookRouter);
91+
92+
app.get('/', (req, res) => {
93+
res.send('welcome to get service');
94+
});
95+
96+
app.listen(port, function() {
97+
console.log('GULP is running my server on PORT: '+port);
98+
});

gulpfile.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const gulp = require('gulp'),
2+
nodemon = require('gulp-nodemon');
3+
4+
gulp.task('default', function(){
5+
nodemon({
6+
script : 'app.js',
7+
ext: 'js',
8+
env: {
9+
PORT: 8000
10+
},
11+
ignore: ['./node_modules/**']
12+
}).on('restart', function(){
13+
console.log('server restarting...');
14+
});
15+
});

models/bookModel.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const mongoose = require('mongoose'),
2+
Schema = mongoose.Schema;
3+
4+
var bookModel = new Schema({
5+
title: {
6+
type: String
7+
},
8+
author: {type: String},
9+
genre: {type: String},
10+
read: {type: Boolean}
11+
});
12+
13+
module.exports = mongoose.model('Book', bookModel);

0 commit comments

Comments
 (0)