The Simplest RESTful server with nodejs, restify, and mongodb
In addition to The Simplest RESTful server with nodejs and restify, this is the more practical solution, but still the simplest way to build nodejs, restify, and mongodb.
First, you need to setup mongodb database and insert data into it.
How To Install & Run MongoDB (Mac OS X)
$ brew update $ brew install mongodb $ mkdir -p ~/data/mongodb $ mongod --fork --dbpath ~/data/mongodb/ --logpath /var/log/mongodb.log # START $ mongo --eval "db.shutdownServer()" admin # STOP
How To Import StarBucks Data To Your Mongodb
$ mongoimport -d starbucks -c stores --type csv --file starbucks_locations.csv --headerline $ mongo # to validate import > show dbs > use starbucks > db.stores.find({"Store ID":1006099})
Starbucks store location data is found at https://opendata.socrata.com/Business/All-Starbucks-Locations-in-the-World/xy4y-c4mk
The following is the server code part. You may need to install mongoose npm install mongoose if you did not.
var restify = require('restify'), // RESTful API Web Server mongoose = require('mongoose'); // mongodb connection /** * config server */ var server = restify.createServer(); server.use(restify.fullResponse()); // enable default headers server.use(restify.bodyParser()); // remap body contentnt of a request to the req.params server.use(restify.queryParser()); // remap query string to req.params /** * config database(starbucks) and model(Store) with collection(stores) */ mongoose.connect('mongodb://localhost/starbucks'); var db = mongoose.connection; db.on('error', function() { console.error('connection error:');} ); db.once('open', function() { console.log('connected to database');} ); // schemaless structure with no restriction to attributes var StoreSchema = mongoose.Schema({any:{}}, {strict:false}) var Store = mongoose.model('stores', StoreSchema); // $ curl -X GET http://localhost:8080/starbucks?country=AE server.get('/starbucks', function(req, res, next) { var attrs = req.params; Store.find(req.params, function(err, stores) { if(err) res.send(500, {error: err}); else res.send(200, stores); return next(); }); return next(); }); // $ curl -X GET http://localhost:8080/starbucks/51 server.get('/starbucks/:id', function(req, res, next) { var storeId = parseInt(req.params.id); Store.findOne({"Store ID":storeId}, function(err, store) { if(err || !store) res.send(404, {error: err||'Not Found'}); else res.send(200, store); return next(); }); }); // $ curl -X POST http://localhost:8080/starbucks -d "Store ID=1234567890" -d "Name=Test" server.post('/starbucks', function(req, res, next) { req.params["Store ID"] = parseInt(req.params["Store ID"]); var store = new Store(req.params); store.save(function (err, store_saved) { if(err) res.send(500, {error: err}); else res.send(201, 'created'); return next(); }); }); // $ curl -X POST http://localhost:8080/starbucks/51 --d "Name=Changed" server.put('/starbucks/:id', function(req, res, next) { var storeId = parseInt(req.params.id); delete req.params.id; Store.update({"Store ID":storeId}, req.params, function (err, store_saved) { if(err) res.send(500, {error: err}); else res.send(200, store_saved); }); return next(); }); // $ curl -X DELETE http://localhost:8080/starbucks/51 server.del('/starbucks/:id', function(req, res, next) { var storeId = parseInt(req.params.id); Store.remove({"Store ID":storeId}, function (err) { if(err) res.send(500, {error: err}); else res.send(204, 'Removed'); }); return next(); }); server.listen(8080, function() { console.log('% listening at %s', server.name, server.url); });
Happy Coding,










