Created
May 12, 2017 06:23
-
-
Save glinesbdev/125f719e8271adb58fd0fd523d839b94 to your computer and use it in GitHub Desktop.
Basic Node / Express API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var adminRouter = require('../config/routers')().adminRouter; | |
var Book = require('../data/models/bookModel'); | |
var db = require('../data/db')(); | |
var adminController = function() { | |
var books = [ | |
{ | |
title: 'War and Peace', | |
author: 'Lev Nikolayevich Tolstoy', | |
genre: 'Historical Fiction', | |
read: false | |
}, | |
{ | |
title: 'The Time Machine', | |
author: 'H. G. Wells', | |
genre: 'Science Fiction', | |
read: false | |
}, | |
{ | |
title: 'The Dark World', | |
author: 'Henry Kuttner', | |
genre: 'Fantasy', | |
read: false | |
} | |
]; | |
adminRouter.route('/book/add') | |
.post(function(req, res) { | |
var book = new Book(req.body); | |
db.addBook(book, function(err, book) { | |
if (err) { | |
res.send(err); | |
} | |
res.json(book); | |
}); | |
}); | |
return adminRouter; | |
}; | |
module.exports = adminController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var middleware = require('./config/middleware'); | |
var app = express(); | |
var port = process.env.port || 3000; | |
middleware(app); | |
app.listen(port, function (err) { | |
if (!err) { | |
console.log('Running on port ' + port); | |
} else { | |
console.error(err); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bookRouter = require('../config/routers')().bookRouter; | |
var Book = require('../data/models/bookModel'); | |
var db = require('../data/db')(); | |
var bookController = function() { | |
var throwError = function(err, req, res) { | |
if (err) { | |
res.send(err); | |
} | |
}; | |
bookRouter.route('/books') | |
.get(function(req, res) { | |
var query = {}; | |
if (req.query.genre) { | |
query.genre = req.query.genre; | |
} | |
db.getBooks(query, function(err, books) { | |
throwError(err, req, res); | |
res.json(books); | |
}); | |
}); | |
bookRouter.route('/book/:id') | |
.get(function(req, res) { | |
var id = req.params.id; | |
db.getBookById(id, function(err, book) { | |
throwError(err, req, res); | |
res.json(book); | |
}); | |
}); | |
return bookRouter; | |
}; | |
module.exports = bookController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var bookModel = new Schema({ | |
title: { | |
type: String | |
}, | |
author: { | |
type: String | |
}, | |
genre: { | |
type: String | |
}, | |
read: { | |
type: Boolean, | |
default: false | |
} | |
}); | |
module.exports = mongoose.model('Book', bookModel); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var bookModel = require('./models/bookModel'); | |
var db = function() { | |
var dbUrl = 'mongodb://localhost:27017/bookAPI'; | |
var Book = bookModel; | |
var addBook = function(book, response) { | |
if (typeof(book) !== 'object') { | |
response('Not a valid book!', null); | |
} else { | |
mongoose.connect(dbUrl); | |
book.save(function(err, book) { | |
response(err, book); | |
mongoose.disconnect(); | |
}); | |
} | |
}; | |
var getBooks = function(query, response) { | |
query = query || {}; | |
mongoose.connect(dbUrl); | |
Book.find(query, function(err, books) { | |
response(err, books); | |
mongoose.disconnect(); | |
}); | |
}; | |
var getBookById = function(id, response) { | |
mongoose.connect(dbUrl); | |
Book.findById(id, function(err, book) { | |
response(err, book); | |
mongoose.disconnect(); | |
}); | |
}; | |
return { | |
addBook: addBook, | |
getBooks: getBooks, | |
getBookById: getBookById | |
}; | |
}; | |
module.exports = db; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var homeRouter = require('../config/routers')().homeRouter; | |
var bookController = function() { | |
homeRouter.route('/') | |
.get(function (req, res) { | |
res.send('Welcome to my API!'); | |
}); | |
return homeRouter; | |
}; | |
module.exports = bookController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var adminController = require('../controllers/adminController')(); | |
var bodyParser = require('body-parser'); | |
var bookController = require('../controllers/bookController')(); | |
var homeController = require('../controllers/homeController')(); | |
var middleware = function(app) { | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use('/', homeController); | |
app.use('/admin', adminController); | |
app.use('/api', bookController); | |
}; | |
module.exports = middleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var routers = function() { | |
var adminRouter = express.Router(); | |
var bookRouter = express.Router(); | |
var homeRouter = express.Router(); | |
return { | |
adminRouter: adminRouter, | |
bookRouter: bookRouter, | |
homeRouter: homeRouter | |
}; | |
}; | |
module.exports = routers; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment