Skip to content

aleclarson/slush

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)