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

organic-express-response

v0.0.3

Published

Organelle for adding default response to incoming requests as expressjs middleware.

Downloads

3

Readme

organic-express-response v0.0.3

Organelle for adding default response to incoming requests as expressjs middleware.

usage with organic-express-routes

module.exports = function(){
  return {
    "GET": function(req, res, next) {
      res.template = "landing"
      next()
    },
    "POST": function(req, res, next) {
      res.body = {success: true}
      next()
    },
    "PUT": function(req, res, next) {
      var not_implemented_error = new Error()
      not_implemented_error.code = 400
      not_implemented_error.body = "not implemented"
      next(not_implemented_error)
    }
  }
}

usage with plain express

// given the express app
var app = express()

// construct express response middleware instance
var plasma = new (require("organic-plasma"))()
require("organic-express-response")(plasma, {reactOn: "ExpressServer"})

// and attach it to express app
plasma.emit({type: "ExpressServer", data: app})

// respond with template
app.get("/", function(req, res, next){
  res.template = "landing"
  next()
})

// respond with raw json data
app.post("/data", function(req, res, next){
  res.body = {success: true}
  next()
})

// respond with custom error
app.get("/failing/route", function(req, res, next){
  var errorFound = new Error()
  errorFound.code = 400
  errorFound.body = "missing argument"
  next(errorFound)
})

The middleware

  • intercepts all requests and sends them as response in case they define response properties
    • or responds with defaults if configured to do so
    • or pass the control flow to followup middleware functions if configured to do so

Optionally the middleware

  • intercepts errors/exceptions from the request - response chain and sends them as response in case they define response error properties
    • or pass the control flow to followup error middleware functions.

response properties

res.template

Does res.render(res.template)

res.body

alias res.response

Sends res.body data either to json or send express res methods.

res.code

Sets res.status

response error properties

err.template

Does res.render(err.template)

err.body

alias err.response

Sends err.body data either to json or send express res methods.

err.code

Sets res.status

dna

{
  "source": "node_modules/organic-express-response",
  "reactOn": "ExpressServer",


  "skipErrorResponses": false,

  "defaultNextRoute": undefined,
  "skipDefaultResponse": true,
  "defaultCode": 404,
  "defaultTemplate": undefined,
  "defaultBody": "not found",

  "skipDefaultErrorResponse": true
  "defaultErrorCode": 500,
  "defaultErrorBody": "error found",

}

reactOn property

Should be either ExpressServer chemical with expected structure or array of chemicals where the first one is mapped as ExpressServer chemical.

defaultCode, defaultTemplate, defaultBody properties

All specify what is the default response if response properties where not found. If defaultTemplate is provided then it will be used instead of defaultBody.

skipDefaultResponse, defaultNextRoute properties

Optional, if set to true default response will not be triggered and the middleware will call next(defaultNextRoute) instead.

skipDefaultErrorResponse

Optional, if set to true will not send default error response

defaultErrorCode, defaultErrorBody properties

All specify what is the default error response code and data when error has been found but it is missing error response properties

skipErrorResponses property

Optional, if set to true will not mount error middleware handler to express app leaving only the middleware for responses without error.