Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A caching proxy server for testing interface of HTTP or HTTPS. A never offline testing interface server. A mock server

NotificationsYou must be signed in to change notification settings

vilien/justreq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NPM versionDownloadsLicense

查看中文版

A caching proxy server for testing interface of HTTP or HTTPS. A never offline testing interface server. A mock server. It can help us to develop offline. It's especially useful for Front-End developers.

Features

  • Each request will be cached. It can make us be absorbed in develop whether online or offline.
  • Mock server. We can use other file to mock interface, such as JSON, TXT and so on.
  • We make an script named JRS, just like PHP. It can mock more flexible interface, and even develop a site.
  • Support for ES6, ES7, can develop more efficient.
  • Support for CORS(Cross-Origin Resource Sharing), so it can work for Web Front-End.
  • No invasive codes, do not inject any code to our project.

Install

InstallNode.js first, then run this to install CLI of JUSTREQ

npm install -g justreq-cli

Install main program using:

npm install justreq

Initialization

To create a configuration file using:

justreq init

After finished, the configuration file ".justreq" will be created at working directory. We can modify it any time. And, we can add comment using JavaScript style.

Usage

To start a JR Server using:

justreq start

Then modify our testing interface to JR Server, eg.

// const API_HOST = "https://test.yourhost.com";constAPI_HOST="http://127.0.0.1:8000";$.get(API_HOST+"/getInfo.do?userId=1001",callback);

Now, all ready, enjoy your coding.


Arguments of CLI

Also, we can run this to clean caches and start JR server

justreq start -c

Or, run this to temporarily proxy other remote not in configuration file

justreq start -h temp.yourhost.com

We can run this to get help

justreq start --help

Advancement

JR Script

Now, I will introduce ourjrs. It's Javascript-based, so we can use it without study any more. Let me show:

// getUser.jrsvaruserId=$_GET['userId'];varusers={1001 :{name:'Bruce',age:22},1002 :{name:'Lily',age:21}};varuser=users[userId];setCookie('userName',user.name);echo(JSON.stringify(user));end();

We added some necessary global function and properties, except usual of JS.

Properties

namedescription
$_GETAn associative object of variables passed to the current script via the URL parameters.
$_POSTAn associative object of variables passed to the current script via the HTTP POST method
$_COOKIEAn associative object of variables passed to the current script via HTTP Cookies.
$_HEADERAn associative object of variables passed to the current script via HTTP headers
$_FILESAn associative object of items uploaded to the current script via the HTTP POST method when using multipart/form-data as the HTTP Content-Type in the request.

Functions

namedescription
echo(string)Output one string.
end([string])To end the current script and output one string.Tips. we must always use this function to finish our script, or it will run until timeout.
sendFile(filepath)Output an file and end the script. Using this, we don't have to use "end()".
setMime(suffix)Modify mimetype of current script. By default, try using "json", switch to "txt" when failure. Options: txt, html, css, xml, json, js, jpg, jpeg, gif, png, svg. Also, we can use "setHeader()" to customize "Content-Type".
setCookie(name, value)Send a cookie. usage: setCookie(name, value [, expires [, path [, domain [, secure [, httponly]]]]])
setHeader(name, value)Send a HTTP header. But, it can't be sent about "Server" and "Date", they are automatic by JR Server.

Forjrs, we can use any modules from Node.js, because it run in Node.js runtime environment. Such asFaker.js,mockjs and so on.And, we can code with ES6, ES7 also, if Node.js enough new.


Rules

For some special interface, we can rule it using:

namedescription
urlThe URL of API, not null. Support for RegExp.
methodThe method of API, Optional. Do not filter any method on default.
ignoreArgsSome fields can be ignored, such as version stamp "?v=1483884433384", we can using:{"ignoreArgs" : "v"}
noCacheNot allow caching. Default is allowed.
subsSubstitution's path. Suggest to use ourjrs, or "json", "txt" and so on.
keepFreshAlways use latest response, unless proxy failed.
hostTemporarily replace host.
portTemporarily replace port.
rewriteThe URL of be rewritten.

Example:

// .justreq{    ..."rules":[{// for default, always try to proxy to keep fresh"url":".+","keepFresh":true},{// using RegExp"url":"user.do\\?id=(\\d+)","subs":"user.jrs?userId=$1"},{"url":"login.do","noCache":true},{"url":"getGoodsInfo.do","ignoreArgs":"v,token,timestamp"},{// all api that include 'system' in url will be proxy to another host"url":"system.+","host":"192.168.1.188","port":"8080"}]}

Inspector

Sometimes, we need another way to decide whether cache the client request or not. Such as follow case. The post data is encode with base64, but a field is insignificant. If we do not intervene to inspect, JUSTREQ will cache repeat.

Examples

// myInsp.jsconstquerystring=require('querystring');constcrypto=require('crypto');constbase64=require('base64-utf8');functionmd5(str){letmd5sum=crypto.createHash('md5');md5sum.update(str);str=md5sum.digest('hex');returnstr;}functioninsp(req,buf){letrawData=buf.toString('utf8');// token=F2F0CF28&encrypt=eyJhcnRpY2xlSWQiOjk5LCJtaXN0IjoiWTJodiJ9letpostData=querystring.parse(rawData);// {token:"F2F0CF28", encrypt:"eyJ..."}if(postData.encrypt){letdecodeString=base64.decode(postData.encrypt);letpayload=JSON.parse(decodeString);// {"articleId":99,"mist":"Y2hv"}letmd5Code=md5(req.method+req.url+payload.articleId);return{needCache:true,cacheId:md5Code};// need to be cached}else{returnnull;// inspector should skip this request}}module.exports=insp;// Must be exported it as node module

And add a configurition to ".justreq"

{..."inspector":".jr/myInsp.js"}

Standard for "insp.js"

/** *@param  {object} req The req create by client request *@param  {buffer} buf The data from FormData *@return {json}       {needCache: <boolean>, cacheId: <md5>} or null */functioninsp(req,buf){  ...return{needCache:<boolean>, cacheId:<md5>};}module.exports=insp;// Must be exported it as node module
Notice:
  • Expect the value of "cacheId" by return as an md5 code. Recommendmd5(req.method + req.url + bufData), to avoid conflict from cache.
  • To skip inspect, we can just return null.
  • To avoid http choke, we can't use any asynchronous code andsetTimeout,setInterval.

Configurations

namedescription
hostNot null, the hostname of remote interface server.
portOptional, the port of remote interface server, default is 80. (Connet interface with HTTPS, if it's 443)
cacheTimeOptional, the time of caches update. value in "h", "m", "s". Default is "20m".
cachePathOptional, cache files directory, default is ".jr/cache".
substitutePathOptional, substitution files directory, default is ".jr/subs".
jrPortOptional, the port of JR server, default is 8000.
proxyTimeoutOptional, the timeout of proxy, value in "h", "m", "s", default is "6s".
proxyHttpsOptional, whether interface server is running on HTTPS or not. Options: "auto", "yes", "no". Default is "auto".
ssl_caOptional, CA(Certificate Authority) path, if interface server is running on HTTPS and need it.
ssl_keyOptional, the path of private key to use for SSL, if interface server is running on HTTPS and need it.
ssl_certOptional, the path of public x509 certificate to use, if interface server is running on HTTPS and need it.
onCorsOptional, on CORS(Cross-Origin Resource Sharing)? Options: "yes", "no". Default is "yes".
inspectorOptional, custom respector script for decide whether cache request or not.
Expect the return of script as{needCache: <boolean>, cacheId: <md5>}
rulesOptional, refer toRULES

DEMO

There are some demo in "./examples" directory.

Open the directory and openrun_examples.cmd(shell./run_examples for Linux) to start JUSTREQ. Also, we can start JUSTREQ usingjustreq start.

Now, we can open any html files to experience.

jrs.html,substitutes.html,upload.html


More

justreq - github

issue

Chinese

blog[CN]

License

Released under theMIT license

About

A caching proxy server for testing interface of HTTP or HTTPS. A never offline testing interface server. A mock server

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp