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

run-middleware

v0.9.10

Published

NodeJS module to execute your Express endpoints (middlewares) from your code. This module will let you laucnch manually all your middleware. It is simulate a client calling to your rest API's. It is not using a network connections

Downloads

4,365

Readme

  • NodeJS run-middleware
  • Why?
  • Installation
  • Support & Contributions
  • Change request paramaters
  • Auto pass cookies
  • Redirecting
  • Changelog
  • Examples
  • License

NodeJS module to execute your Express endpoints (middlewares) from your code. This module will let you manually launch all your middleware. It is simulating a client calling your rest APIs, without using a network connection (your server does not even need to listen on a port).

#NodeJS run-middleware

npm npm version Join the chat at https://gitter.im/node-run-middleware/Lobby Build Status

Why?

Many times, your server and your client, need to execute the same functions. For example here is an endpoint to get user details:

app.get('/get-user/:id',function(req,res){
	mysql.query('select * from users where id=?',[req.params.id],function(err,rows){
		res.send({user:rows[0]})
	})
})

Now you want to get the user details from your code. What should you do?

app.runMiddleware('/get-user/20',function(code,body,headers){
	console.log('User Details:',body)
})

Installation

npm i -S run-middleware

var express=require('express')
var app=express();
require('run-middleware')(app)

Support & Contributions

  • Pull requests, issues, and English proofreading are welcome on Github.
  • Question & support on StackOverflow using run-middlewaretag.

Change request paramaters

As options you can pass the query, body, method, cookies parameters.

app.runMiddleware('/handler',{
		method:'post',
		query:{token:'tk-12345'},
		body:{"action":"list","path":"/"}
	},function(code,data){
		console.log(code,data)
		process.exit()
	})

Auto pass cookies

When you can runMiddleware from another middleware, you can autopass all the parameters of the current middleware, by using the express request object.

app.get('/middleware1',function(req,res){
	// We use res.runMiddleware instead of app.runMiddleware. All the cookies & other data (like socket.io session) will be pass to the second middleware
	res.runMidleware(...)
})

Redirecting

If the middleware you execute will redirect, you will be notified about it, by reading the code and the headers.location

app.runMiddleware('/this-middleware-will-response-as-redirect',function(code,body,headers){
	if(code==301 || code=302) {// Redirect HTTP codes
		console.log('Redirect to:',headers.location)
	}
})

Changelog

  • v1.0.0 (25 June 2018) -
  • v0.6.1 (9 Sep 2016) - Supports response.redirect
  • v0.6.2 (10 Sep 2016) - Supports passing cookies and other variables to runMiddleware
  • v0.6.3 (11 Sep 2016) - Supports running middleware from others middleware, for automatically passing cookies and headers between middlewares.
  • v0.6.4 (13 Sep 2016) - Better documentation and examples

Examples

See the tests

License

ISC License Copyright (c) 2016, Aminadav Glickshtein

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.