1.0.4 • Published 10 years ago

express-rest v1.0.4

Weekly downloads
9
License
Apache-2
Repository
github
Last release
10 years ago

express-rest

Build Downloads Version License

Simplest possible REST implementation for Node / Express.

  • Uses familiar syntax for those who use express
  • Handles serialization and content-types (XML coming soon)
  • Embrace HTTP

Installation

npm install express-rest

Usage

var express = require('express'),
    expressRest = require('express-rest');

var app = express();
var rest = expressRest(app);

var records = [
    {value: 'Apple'},
    {value: 'Banana'}
];

rest.get('/api/food', function(req, rest) {
    rest.ok(records);
});

rest.get('/api/food/:id', function(req, rest) {
    var record = records[req.params.id];
    if (record) rest.ok(record);
    else rest.notFound();
});

rest.put('/api/food/:id', function(req, rest) {
    records[req.params.id] = req.body;
    return rest.accepted('/api/food/' + encodeURI(req.params.id));
});

rest.post('/api/food', function(req, rest) {
    records.push(req.body);
    rest.created('/api/food/' + (records.length - 1));
});

rest.delete('/api/food/:id', function(req, rest) {
    delete records[req.params.id];
    rest.gone();
})


app.listen();

Custom MIME Types

var express = require('express'),
    expressRest = require('express-rest');

var app = express();
var rest = expressRest(app, {
    serializers: {
        'text/yaml': {
            deserialize: function(req, rest, next) {
                req.body = object;
                next();
            },
            serialize: function(req, rest, next) {
                rest.send(buffer);
                next();
            }
        }
    }
});

Response functions

Each HTTP response code is conveniently wrapped in an appropriately named function. Depending on the status, the parameter can be body (state object to be returned), location (URI to the resource), or message (string to describe an error).

Function#Status TextParameter
continue100Continue
switchingProtocols101Switching Protocols
checkpoint103Checkpoint
ok200OKBody
created201CreatedLocation
accepted202AcceptedLocation
nonAuthoritativeInformation203Non Authoritative InformationBody
noContent204No Content
resetContent205Reset Content
partialContent206Partial ContentBody
multipleChoices300Multiple ChoicesBody
movedPermanently301Moved PermanentlyLocation
found302FoundLocation
seeOther303See OtherLocation
notModified304Not Modified
switchProxy306Switch Proxy
temporaryRedirect307Temporary RedirectLocation
resumeIncomplete308Resume IncompleteBody
badRequest400Bad RequestMessage
unauthorized401UnauthorizedMessage
paymentRequired402Payment RequiredMessage
forbidden403ForbiddenMessage
notFound404Not FoundMessage
methodNotAllowed405Method Not AllowedMessage
notAcceptable406Not AcceptableMessage
proxyAuthenticationRequired407Proxy Authentication RequiredMessage
requestTimeout408Request TimeoutMessage
conflict409ConflictMessage
gone410GoneMessage
lengthRequired411Length RequiredMessage
preconditionFailed412Precondition FailedMessage
requestEntityTooLarge413Request Entity Too LargeMessage
requestURITooLong414Request URI Too LongMessage
unsupportedMediaType415Unsupported Media TypeMessage
requestedRangeNotSatisfiable416Requested Range Not SatisfiableMessage
expectationFailed417Expectation FailedMessage
internalServerError500Internal Server ErrorMessage
notImplemented501Not ImplementedMessage
badGateway502Bad GatewayMessage
serviceUnavailable503Service UnavailableMessage
gatewayTimeout504Gateway TimeoutMessage
httpVersionNotSupported505HTTP Version Not SupportedMessage
networkAuthenticationRequired511Network Authentication RequiredMessage