Skip to content

Instantly share code, notes, and snippets.

@greggnakamura
Created April 1, 2015 02:12
Show Gist options
  • Save greggnakamura/6eeb4710e88776869b8c to your computer and use it in GitHub Desktop.
Save greggnakamura/6eeb4710e88776869b8c to your computer and use it in GitHub Desktop.
Javascript: REST Level 2 w/Status Codes example (Nettuts: How to Build a Hypermedia-Driven REST API)
var express = require('express');
var app = express();
var Datastore = require('nedb');
var db = {};
var responder = require('./httpResponder');
var port = process.argv[2] || 3050;
var root = 'http://localhost:' + port;
// Connect to an NeDB database
db.movies = new Datastore({ filename: 'db/movies', autoload: true });
// add an index
db.movies.ensureIndex({ fieldName: 'title', unique: true });
// Necessary for accessing POST data via req.body object
app.use(express.bodyParser());
// Catch-all route to set global values
app.use(function (req, res, next) {
res.type('application/json');
res.locals.respond = responder.setup(res);
next();
});
// Routes
app.get('/', function (req, res) {
res.send('The API is working.');
});
app.get('/movies', function (req, res) {
db.movies.find({}, res.locals.respond);
});
app.post('/movies', function (req, res) {
// handle client error
// http://www.screencast.com/t/iJImQ3qEF
if (!req.body.title) {
res.json(400, { error: { message: 'A title is required to create new movie.' } });
return;
}
// handle server error
// http://www.screencast.com/t/p0vQW0fTVHEg
db.movies.insert({ title: req.body.title }, function (err, created) {
if (err) {
res.json(500, { error: err });
return;
}
// set location header
// http://www.screencast.com/t/C6jvMPAekaRU
res.set('Location', root + '/movies/' + created._id);
res.json(201, created);
});
});
app.get('/movies/:id', function (req, res) {
db.movies.findOne({ _id: req.params.id }, function (err, result) {
if (err) {
res.json(500, { error: err });
return;
}
if (!result) {
res.json(400, { error: { message: 'We did not find a movie with id: ' + req.params.id } });
return;
}
res.json(200, result);
});
});
app.put('/movies/:id', function (req, res) {
db.movies.update({ _id: req.params.id }, req.body, function (err, num) {
res.locals.respond(err, { success: num + " records updated" });
});
});
app.delete('/movies/:id', function (req, res) {
db.movies.remove({ _id: req.params.id }, function (err, num) {
if (err) {
res.json(500, { error: err });
return;
}
if (num === 0) {
res.json(404, { error: { message: 'We did not find a movie with id: ' + req.params.id } });
return;
}
// http://www.screencast.com/t/A3KdbpNJ
res.set('Link', root + '/movies; rel="collection"');
res.send(204);
});
});
app.listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment