GithubHelp home page GithubHelp logo

dustinbolton / node-run-middleware Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aminadav/node-run-middleware

0.0 1.0 0.0 156 KB

NodeJS Express module to simulate URL requests, for internal executing REST API's

Home Page: https://github.com/aminag/node-run-middleware

License: ISC License

JavaScript 100.00%

node-run-middleware's Introduction

NodeJS run-middleware

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).

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', (req, res) => {
  mysql.query('select * from users where id=?', [req.params.id], (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', (_, body) => {
  console.log(`User details: ${body}`);
});

Installation

npm i -S run-middleware
const express = require('express');
const app = express();
const runMiddleware = require('run-middleware');

runMiddleware(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 and cookies parameters.

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

Auto pass cookies

When you runMiddleware from another location, you don't have to pass all the parameters of the current middleware to the handler.

app.get('/middleware1', (req, res) => {
  req.runMiddleware( /* ... */ );
})

Redirecting

You can check if the middleware executed will redirect the request by checking the code and the headers.location fields.

app.runMiddleware('/this-middleware-will-response-as-redirect', (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 for further examples.

node-run-middleware's People

Contributors

aminadav avatar dependabot[bot] avatar aminag avatar alice-613 avatar dustinbolton avatar therealpecus avatar dvdvdmt avatar jneidel avatar nicksulkers avatar hotlib avatar gitter-badger avatar

Watchers

 avatar

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.