├── app └── models │ └── bear.js ├── package.json ├── README.md ├── .gitignore └── server.js /app/models/bear.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | var Schema = mongoose.Schema; 3 | 4 | var BearSchema = new Schema({ 5 | name: String 6 | }); 7 | 8 | module.exports = mongoose.model('Bear', BearSchema); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-api", 3 | "main": "server.js", 4 | "repository": { 5 | "type": "git", 6 | "url": "git://github.com/scotch-io/node-token-authentication" 7 | }, 8 | "scripts": { 9 | "start": "node server.js" 10 | }, 11 | "author": "Chris Sevilleja ", 12 | "license": "MIT", 13 | "dependencies": { 14 | "express": "~4.16.1", 15 | "morgan": "~1.9.0", 16 | "mongoose" : "~4.11.14", 17 | "body-parser": "~1.18.2" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Building a RESTful API in Node and Express 2 | 3 | Using the new Express 4.0 Router to build an API 4 | 5 | [Read the tutorial](http://scotch.io/tutorials/javascript/build-a-restful-api-using-node-and-express-4) 6 | 7 | ## Requirements 8 | 9 | - Node and npm 10 | 11 | ## Installation 12 | 13 | - Clone the repo: `git clone git@github.com:scotch-io/node-api` 14 | - Install dependencies: `npm install` 15 | - Start the server: `node server.js` 16 | 17 | ## Testing the API 18 | Test your API using [Postman](https://chrome.google.com/webstore/detail/postman-rest-client-packa/fhbjgbiflinjbdggehcddcbncdddomop) 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Deployed apps should consider commenting this line out: 24 | # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | // BASE SETUP 2 | // ============================================================================= 3 | 4 | // call the packages we need 5 | var express = require('express'); 6 | var bodyParser = require('body-parser'); 7 | var app = express(); 8 | var morgan = require('morgan'); 9 | 10 | // configure app 11 | app.use(morgan('dev')); // log requests to the console 12 | 13 | // configure body parser 14 | app.use(bodyParser.urlencoded({ extended: true })); 15 | app.use(bodyParser.json()); 16 | 17 | var port = process.env.PORT || 8080; // set our port 18 | 19 | // DATABASE SETUP 20 | var mongoose = require('mongoose'); 21 | mongoose.connect('mongodb://node:node@novus.modulusmongo.net:27017/Iganiq8o'); // connect to our database 22 | 23 | // Handle the connection event 24 | var db = mongoose.connection; 25 | db.on('error', console.error.bind(console, 'connection error:')); 26 | 27 | db.once('open', function() { 28 | console.log("DB connection alive"); 29 | }); 30 | 31 | // Bear models lives here 32 | var Bear = require('./app/models/bear'); 33 | 34 | // ROUTES FOR OUR API 35 | // ============================================================================= 36 | 37 | // create our router 38 | var router = express.Router(); 39 | 40 | // middleware to use for all requests 41 | router.use(function(req, res, next) { 42 | // do logging 43 | console.log('Something is happening.'); 44 | next(); 45 | }); 46 | 47 | // test route to make sure everything is working (accessed at GET http://localhost:8080/api) 48 | router.get('/', function(req, res) { 49 | res.json({ message: 'hooray! welcome to our api!' }); 50 | }); 51 | 52 | // on routes that end in /bears 53 | // ---------------------------------------------------- 54 | router.route('/bears') 55 | 56 | // create a bear (accessed at POST http://localhost:8080/bears) 57 | .post(function(req, res) { 58 | 59 | var bear = new Bear(); // create a new instance of the Bear model 60 | bear.name = req.body.name; // set the bears name (comes from the request) 61 | 62 | bear.save(function(err) { 63 | if (err) 64 | res.send(err); 65 | 66 | res.json({ message: 'Bear created!' }); 67 | }); 68 | 69 | 70 | }) 71 | 72 | // get all the bears (accessed at GET http://localhost:8080/api/bears) 73 | .get(function(req, res) { 74 | Bear.find(function(err, bears) { 75 | if (err) 76 | res.send(err); 77 | 78 | res.json(bears); 79 | }); 80 | }); 81 | 82 | // on routes that end in /bears/:bear_id 83 | // ---------------------------------------------------- 84 | router.route('/bears/:bear_id') 85 | 86 | // get the bear with that id 87 | .get(function(req, res) { 88 | Bear.findById(req.params.bear_id, function(err, bear) { 89 | if (err) 90 | res.send(err); 91 | res.json(bear); 92 | }); 93 | }) 94 | 95 | // update the bear with this id 96 | .put(function(req, res) { 97 | Bear.findById(req.params.bear_id, function(err, bear) { 98 | 99 | if (err) 100 | res.send(err); 101 | 102 | bear.name = req.body.name; 103 | bear.save(function(err) { 104 | if (err) 105 | res.send(err); 106 | 107 | res.json({ message: 'Bear updated!' }); 108 | }); 109 | 110 | }); 111 | }) 112 | 113 | // delete the bear with this id 114 | .delete(function(req, res) { 115 | Bear.remove({ 116 | _id: req.params.bear_id 117 | }, function(err, bear) { 118 | if (err) 119 | res.send(err); 120 | 121 | res.json({ message: 'Successfully deleted' }); 122 | }); 123 | }); 124 | 125 | 126 | // REGISTER OUR ROUTES ------------------------------- 127 | app.use('/api', router); 128 | 129 | // START THE SERVER 130 | // ============================================================================= 131 | app.listen(port); 132 | console.log('Magic happens on port ' + port); 133 | --------------------------------------------------------------------------------