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

middleware-proxy

v2.0.5

Published

A connect-compliant middleware for simple, transparent redirection of HTTP requests.

Downloads

170

Readme

middleware-proxy

This is a simple tool that smooths the difficulties of working on integrated projects where the backend is hosted elsewhere from where you are developing your frontend. The proxy allows you to access your static HTML through a node server and redirect your RESTful calls to the implementing server.

usage

const proxy = require('middleware-proxy');

/**
 * Returns a middleware function that selectively proxies incoming requests to another server.
 *
 * @param  {string|RegExp} matcher - used to detect if the middleware should be applied against the request URL
 * @param  {string} server - a fully qualified server address, e.g. http://localhost:8080
 * @param  {string} [path_to_strip] - (optional) a leading string that should be removed from the request URI
 *
 * @return {function} middleware, suitable for use in express or connect
 */
proxy('/api/v1', 'http://localhost:8080')`

basic express example

const express = require('express');
const app = express();
const http = require('http');
const proxy = require('middleware-proxy');

app.configure(function() {
    app.set('port', process.env.PORT || 3000);
    app.use(express.static(__dirname + '/app'));
});

app.use(proxy('/service', 'http://localhost:8080'));

http.createServer(app).listen(app.get('port'), function() {
    console.log(`Express server listening on port ${app.get('port')}`);
});

This will send all requests starting with /service to http://localhost:8080/service.

using budo (a browserify livereload solution)

const proxy = require('middleware-proxy');

require('budo')('index.js', {
    browserify: {
        debug: true,
    },

    host: '0.0.0.0',
    live: true,
    open: true,
    portfind: true,
    pushstate: true,
    serve: 'assets/bundle.js',
    stream: process.stdout,

    middleware: [proxy('/service', 'http://localhost:8080')],
});

removing an API prefix

Let's say your integration environment has chosen /api as the mount point for communicating with your application's backend APIs. However, you're building your frontend in a separate project and can't easily run a dev server that joins both functionalities. middleware-proxy solves this issue by allowing a third optional parameter that defines the part of the request URL to be stripped away when proxying the request to your destination host.

Assuming your remote development API host is at http://localhost:8080 and has a RESTful collection called /posts, you would write your AJAX calls to GET from /api/posts and use the following configuration:

proxy('/api/posts', 'http://localhost:8080', '/api');

The request is transparently redirected behind the scenes:

GET /api/posts -> GET http://localhost:8080/posts

Based on the work in rgaskill/dev-rest-proxy