Code Monkey home page Code Monkey logo

express-conditional-request's Introduction

Conditional Requests

This is a small extenstion to Richard Schneider's express-preconditions package.

Middleware for HTTP Conditional Requests (RFC 7232). It conditionally processes a HTTP request based on a precondition (such as an etag or modification date). A precondition is specified using the If-Match, If-None-Match, If-Modified-Since or If-Unmodified-Since HTTP header.

The change log is automatically produced with the help of semantic-release.

Getting started

Install with npm

> npm install express-preconditions-addl --save

Usage

Include the following in your server startup

    const preconditions = require('express-preconditions-addl')
    app.use(preconditions())

or

    const preconditions = require('express-preconditions-addl')
    const options = { ... }
    app.use(preconditions(options))

See our test server for more details.

Options

Name Description
error A function that takes (status code, message, req, res) and sends an error response.
requiredWith An array of HTTP methods that must include one of the precondition headers. This prevents the lost update issue. Defaults to ['PUT', 'PATCH', 'DELETE'].
stateAsync A function that takes (req) and returns a Promise to the get the assoiciated resource state.
forwardHeaders Additional option added to existing express-preconditions package to support sending custom headers to HEAD request.Defaults to []

Resource state

The resource state contains the etag and lastModified properties, which conform to the respective HTTP headers and can be undefined.

The default stateAsync() obtains the resource state by sending a HEAD request to the server.

Status codes

The following HTTP status codes can be returned by the middleware

Code Reason
304 (Not Modified) The resource has not been modified since the version specified by the precondition(s).
412 (Precondition Failed) A precondition failed.
428 (Precondition Required) A precondition header is required to avoid the lost update issue.
501 (Not implemented) A precondition is not yet implemented.

TODO

  • The If-Range header is not currently supported.

License

The MIT license.

express-conditional-request's People

Contributors

maheshmahadevan avatar richardschneider avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.