Giter VIP home page Giter VIP logo

packing-urlrewrite's Introduction

packing-urlrewrite

一个用来做代理和转发URL的express中间件

NPM

安装

npm install packing-urlrewrite --save-dev

使用

使用静态配置(静态和热加载配置二选一)

import Express from 'express';
import urlrewrite from 'packing-urlrewrite';

const rules = {
  // 1.用json模拟数据,标示符为 `require!`
  '^/api/(.*)': 'require!/mock/api/$1.js',
  // 2.同域转发
  '^/$': '/index.html',
  // 3.跨域转发
  '^/test/(.*)': 'http://test.xxx.com/test/$1'
};
const app = new Express();
app.use(urlrewrite(rules));
// /mock/api/$1.js
export default (req, res) => {
  // maybe get parameters from request
  const data = {
    name: 'Joe'
  };
  res.setHeader('Content-Type', 'application/json');
  res.end(JSON.stringify(data));
};

使用热加载配置

import Express from 'express';
import urlrewrite from 'packing-urlrewrite';

const rules = {
  // 用热发配置rulesHotFile
  rulesHotFile: __dirname + '/rewriteRules.js'
};
const app = new Express();
app.use(urlrewrite(rules));
// rewriteRules.js
module.exports = {
  // 1.用json模拟数据,标示符为 `require!`
  '^/api/(.*)': 'require!/mock/api/$1.js',
  // 2.同域转发
  '^/$': '/index.html',
  // 3.跨域转发
  '^/test/(.*)': 'http://test.xxx.com/test/$1'
}

packing-urlrewrite's People

Contributors

zhongzhi107 avatar gotolu avatar

Watchers

James Cloos 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.