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

koa2-remote

v1.0.3

Published

load data from remote url.

Downloads

4

Readme

从远程服务器加载数据

  • 从远程加载数据
  • 转发并处理header,避免header非法异常(例如多余空格等)
  • 取消gzip压缩,避免解析问题(待改进)
  • 加载文本内容(html,css,js)
  • 加载二进制内容(图片)

安装

npm install koa@next --save
npm install koa2-remote --save

使用

var koa = require('koa');
var app = new koa();

var remote = require('koa2-remote');

app.use(function (ctx, next) {
  var req = ctx.request;
  console.log('url:', req.protocol + '://' + req.host + req.url);
  // 修改host,后面转发地址将使用新的host地址(header内容也将被修改)
  req.header.host = 'laomu1988.github.io';
  return next();
});

// 判断是否存在数据,假如不存在数据则从远程加载
app.use(remote());

app.listen(3000, function (err) {
  err && console.log(err) || console.log('start server at http://localhost:3000/index.html');
});

假如想把请求转发到某台机器上,但是不修改其中的header,可以这样配置

app.use(function(ctx) {
    ctx.remote = ctx.remote || {};
    ctx.remote.host = 'otherhost'; // 只修改host
    // 或者ctx.remote = ctx.protocol + '://otherhost' + ctx.url  // 直接计算新的请求地址
    // ctx.remote.url = 'otherurl';  // 只修改请求url
    // ctx.remote.protocol = 'https';  // 只修改请求protocol
});

ctx.remote可以是一个false、字符串或者plain object - 假如是false将不会进行转发 - 是字符串时,将直接请求这个地址 - 是plain object时,假如其中定义有protocol,host,url,则将使用配合ctx.request重新计算新的请求地址

todo:

  • [x] url中是否包含端口: header的host包含端口,hostname不包含端口
  • [x] response是否包含status: statusCode
  • [x] 向某一个地址转发请求但是host保持原样(不修改header中的host): 增加ctx.remote表示远程请求地址
  • [ ] gzip压缩与解压