Giter Site home page Giter Site logo

slush's Introduction

slush v3.2.1 stable

Lightweight replacement to express.

slush = require "slush"

app = slush()

app.ready ->
  console.log "Listening at http://localhost:" + app.port

Options

  • sock: String? Listen on a socket path
  • port: Number? If undefined, default to process.env.PORT, or 443 (if HTTPS), or 8000 (if HTTP)
  • secure: Boolean? If true, an HTTPS server is created with ssl.key and ssl.crt from the project root
  • maxHeaders: Number? Limit the number of headers (defaults to 50)
  • timeout: Number? The timeout (in ms) before a request sends a 408 response
  • onError: Function? Send a custom response when your server throws an unexpected error
  • onUnhandled: Function? Send a custom response when a request goes unhandled

Request handling

The slush server uses a pipeline of request handlers (called "pipes").

app.pipe (req, res) ->
  # Handle the request in here.
  # Calls to `pipe` can be chained.

The return value of each pipe is used to determine the server's next action.

Return undefined or null to skip the rest of the current pipe.

Return a Number literal to set the status code for an empty response.

Return an Error instance for invalid requests.

Return a Promise instance for asynchronous requests. The resolved value is treated the same as synchronous return values.

The server will visit each pipe in the pipeline until res.send is called. If that never happens, the server responds with "404 Not Found".

If an Error instance is thrown while inside a pipe, the server responds with "500 Internal Error".

Express middleware

Support for express middleware is included.

app.use middleware

The drain method

Use the drain() method for running code after the response is sent.

app.drain (req, res) ->
  # The response has been sent.

Server events

  • request: (req, res) A request was received
  • response: (req, res) A response has finished
  • requestError: (error) The request handler caught an error (not emitted when options.onError is defined)
  • error: () A server error occurred (see here)
  • close: () The server has closed, all connections have ended (see here)

slush's People

Contributors

aleclarson avatar

Stargazers

 avatar

Watchers

 avatar  avatar

slush's Issues

Measuring response times

Here's an example of measuring response times:

const app = slush()

app.on('response', (req, res) => {
  console.log(`${res.statusCode} ${req.method} ${req.path} ${req.elapsedTime}ms`)
  // => 200 GET /users 16ms
})

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.