Skip to content

Instantly share code, notes, and snippets.

@damianesteban
Created November 21, 2014 18:25
Show Gist options
  • Save damianesteban/7e9511ec794dab085ac2 to your computer and use it in GitHub Desktop.
Save damianesteban/7e9511ec794dab085ac2 to your computer and use it in GitHub Desktop.
server
// server.js
// BASE SETUP
// ==================================================================
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using Express
var bodyParser = require('body-parser');
var mongoose = require('mongoose'); // Our Database
var Bear = require('./app/models/bear');
mongoose.connect('mongodb://user:user@mongolab.com') // connect to our DB
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR OUR API
// ==================================================================
var router = express.Router();
router.use(function(req, res, next) {
// do logging
console.log('Something is happening...');
next(); // ensures we go to the next routes and don't stop here
});
// test route to make sure everything is working (accessed at
// GET http://localhost/api:8080)
router.get('/', function(req, res) {
res.json({
message: 'Hooray! Welcome to our API!'
});
});
// more routes for our API will happen here
// on routes that end in /bears
router.route('/bears')
// create a bear (accessed at POST http://localhost:8080/api/bears)
.post(function(req, res) {
var bear = new Bear(); // create a new instance of the Bear model
bear.name = req.body.name; // set the bears name (comes from the request)
// save the bear and check for errors
bear.save(function(err) {
if (err) res.send(err);
res.json({ message: 'Bear created!' });
});
})
// get all the bears (access at GET http://local.dev:8080/api/bears
.get(function(req, res) {
Bear.find(function(err, bears) {
if (err)
res.send(err);
res.json(bears);
});
});
// on routes that end in /bears/:bear_id
//==========================================
router.route('/bears/:bear_id')
// get the bear with that id (accessed at GET http://local.dev:8080/api/bears/bear_id)
.get(function(req, res) {
Bear.findById(req.params.bear_id, function(err, bear) {
if (err)
res.send(err);
res.json(bear);
});
})
// update the bear with this id (accessed at PUT http://local.dev:8080/api/bears/:bear
.put(function(req, res) {
// use our bear model to find the bear we want
Bear.findById(req.params.bear_id, function(err, bear) {
if (err)
res.send(err);
bear.name = req.body.name; // update the bears info
// save the bear
bear.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Bear updated!' });
});
});
})
// delete the bear with this id (access at DELETE http://local.dev:8080/api/bears/:bear_id
.delete(function(req, res) {
Bear.remove({
_id: req.params.bear_id
}, function(err, bear) {
if (err)
res.send(err);
res.json({ message: 'Successfully deleted' });
});
});
// REGISTER OUR ROUTES ---------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// ===================================================================
app.listen(port);
console.log('The magic happens on port ' + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment