npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-rest

v1.0.4

Published

Simple REST for Node with Express

Downloads

14

Readme

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 Text |Parameter |-------------------------------|---|-------------------------------|---------| |continue |100|Continue | | |switchingProtocols |101|Switching Protocols | | |checkpoint |103|Checkpoint | | |ok |200|OK |Body | |created |201|Created |Location | |accepted |202|Accepted |Location | |nonAuthoritativeInformation |203|Non Authoritative Information |Body | |noContent |204|No Content | | |resetContent |205|Reset Content | | |partialContent |206|Partial Content |Body | |multipleChoices |300|Multiple Choices |Body | |movedPermanently |301|Moved Permanently |Location | |found |302|Found |Location | |seeOther |303|See Other |Location | |notModified |304|Not Modified | | |switchProxy |306|Switch Proxy | | |temporaryRedirect |307|Temporary Redirect |Location | |resumeIncomplete |308|Resume Incomplete |Body | |badRequest |400|Bad Request |Message | |unauthorized |401|Unauthorized |Message | |paymentRequired |402|Payment Required |Message | |forbidden |403|Forbidden |Message | |notFound |404|Not Found |Message | |methodNotAllowed |405|Method Not Allowed |Message | |notAcceptable |406|Not Acceptable |Message | |proxyAuthenticationRequired |407|Proxy Authentication Required |Message | |requestTimeout |408|Request Timeout |Message | |conflict |409|Conflict |Message | |gone |410|Gone |Message | |lengthRequired |411|Length Required |Message | |preconditionFailed |412|Precondition Failed |Message | |requestEntityTooLarge |413|Request Entity Too Large |Message | |requestURITooLong |414|Request URI Too Long |Message | |unsupportedMediaType |415|Unsupported Media Type |Message | |requestedRangeNotSatisfiable |416|Requested Range Not Satisfiable|Message | |expectationFailed |417|Expectation Failed |Message | |internalServerError |500|Internal Server Error |Message | |notImplemented |501|Not Implemented |Message | |badGateway |502|Bad Gateway |Message | |serviceUnavailable |503|Service Unavailable |Message | |gatewayTimeout |504|Gateway Timeout |Message | |httpVersionNotSupported |505|HTTP Version Not Supported |Message | |networkAuthenticationRequired |511|Network Authentication Required|Message |