mirror of
https://github.com/danbulant/koa-plugins
synced 2026-05-24 12:22:19 +00:00
Fix readme tables & add automatic mime types to static
This commit is contained in:
parent
70ece68ce6
commit
9f1cb532e5
4 changed files with 26 additions and 16 deletions
11
README.md
11
README.md
|
|
@ -11,8 +11,9 @@ Easy plugins is based on Koa, so the ctx variable that's passed is koa's context
|
||||||
|
|
||||||
The plugin must export object or function. Object should contain these:
|
The plugin must export object or function. Object should contain these:
|
||||||
|
|
||||||
| enabled | Whether the plugin is enabled and should be used |
|
| Name | Descripption |
|
||||||
| ---- | ---- |
|
| ---- | ---- |
|
||||||
|
| enabled | Whether the plugin is enabled and should be used |
|
||||||
| exec | Function to call when plugin is enabled and request matches rules |
|
| exec | Function to call when plugin is enabled and request matches rules |
|
||||||
| rules | Object containing rules to be passed before calling exec. Can be function, in which case the context is passed and must return boolean (or Promise<boolean>). If empty, automatically evaluates to true.|
|
| rules | Object containing rules to be passed before calling exec. Can be function, in which case the context is passed and must return boolean (or Promise<boolean>). If empty, automatically evaluates to true.|
|
||||||
|
|
||||||
|
|
@ -20,14 +21,16 @@ If the exported is function, it works as `multi-plugin`:
|
||||||
|
|
||||||
The function gets parameter `hooks` which is a class for managing plugins. Then it should call function `hooks.registerPlugin` with the following parameters:
|
The function gets parameter `hooks` which is a class for managing plugins. Then it should call function `hooks.registerPlugin` with the following parameters:
|
||||||
|
|
||||||
| exec | Function to call when request rules matches |
|
|
||||||
| ---- | ---- |
|
* `exec` Function to call when request rules matches
|
||||||
| rules | *optional*, same as in object plugin |
|
* `rules` *optional*, same as in object plugin
|
||||||
|
|
||||||
### Rules
|
### Rules
|
||||||
|
|
||||||
Following rules can be used
|
Following rules can be used
|
||||||
|
|
||||||
|
| Rule | Type | Description |
|
||||||
|
| ---- | ---- | ----------- |
|
||||||
| path | String or String[] | If path matches **exactly**. |
|
| path | String or String[] | If path matches **exactly**. |
|
||||||
| pathReg | Regex | Regex to be checked against (must be instance of regex) |
|
| pathReg | Regex | Regex to be checked against (must be instance of regex) |
|
||||||
| method | String or String[] | Method used |
|
| method | String or String[] | Method used |
|
||||||
|
|
|
||||||
16
node_modules/mime-types/package.json
generated
vendored
16
node_modules/mime-types/package.json
generated
vendored
|
|
@ -1,29 +1,31 @@
|
||||||
{
|
{
|
||||||
"_from": "mime-types@~2.1.24",
|
"_from": "mime-types",
|
||||||
"_id": "mime-types@2.1.26",
|
"_id": "mime-types@2.1.26",
|
||||||
"_inBundle": false,
|
"_inBundle": false,
|
||||||
"_integrity": "sha512-01paPWYgLrkqAyrlDorC1uDwl2p3qZT7yl806vW7DvDoxwXi46jsjFbg+WdwotBIk6/MbEhO/dh5aZ5sNj/dWQ==",
|
"_integrity": "sha512-01paPWYgLrkqAyrlDorC1uDwl2p3qZT7yl806vW7DvDoxwXi46jsjFbg+WdwotBIk6/MbEhO/dh5aZ5sNj/dWQ==",
|
||||||
"_location": "/mime-types",
|
"_location": "/mime-types",
|
||||||
"_phantomChildren": {},
|
"_phantomChildren": {},
|
||||||
"_requested": {
|
"_requested": {
|
||||||
"type": "range",
|
"type": "tag",
|
||||||
"registry": true,
|
"registry": true,
|
||||||
"raw": "mime-types@~2.1.24",
|
"raw": "mime-types",
|
||||||
"name": "mime-types",
|
"name": "mime-types",
|
||||||
"escapedName": "mime-types",
|
"escapedName": "mime-types",
|
||||||
"rawSpec": "~2.1.24",
|
"rawSpec": "",
|
||||||
"saveSpec": null,
|
"saveSpec": null,
|
||||||
"fetchSpec": "~2.1.24"
|
"fetchSpec": "latest"
|
||||||
},
|
},
|
||||||
"_requiredBy": [
|
"_requiredBy": [
|
||||||
|
"#USER",
|
||||||
|
"/",
|
||||||
"/accepts",
|
"/accepts",
|
||||||
"/cache-content-type",
|
"/cache-content-type",
|
||||||
"/type-is"
|
"/type-is"
|
||||||
],
|
],
|
||||||
"_resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.26.tgz",
|
"_resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.26.tgz",
|
||||||
"_shasum": "9c921fc09b7e149a65dfdc0da4d20997200b0a06",
|
"_shasum": "9c921fc09b7e149a65dfdc0da4d20997200b0a06",
|
||||||
"_spec": "mime-types@~2.1.24",
|
"_spec": "mime-types",
|
||||||
"_where": "/home/dan/Documents/node-fw/node_modules/accepts",
|
"_where": "/home/dan/Documents/node-fw",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/jshttp/mime-types/issues"
|
"url": "https://github.com/jshttp/mime-types/issues"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
10
package.json
10
package.json
|
|
@ -1,17 +1,21 @@
|
||||||
{
|
{
|
||||||
"name": "node-fw",
|
"name": "node-fw",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "Plugin-based webserver for node",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "Daniel Bulant",
|
||||||
|
"repository": {
|
||||||
|
"url": "https://github.com/danbulant/koa-plugins"
|
||||||
|
},
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chalk": "^3.0.0",
|
"chalk": "^3.0.0",
|
||||||
"fastcgi-client": "0.0.1",
|
"fastcgi-client": "0.0.1",
|
||||||
"koa": "^2.11.0"
|
"koa": "^2.11.0",
|
||||||
|
"mime-types": "^2.1.26"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path")
|
const path = require("path")
|
||||||
const PassThrough = require('stream').PassThrough;
|
const PassThrough = require('stream').PassThrough;
|
||||||
|
const mime = require('mime-types');
|
||||||
|
|
||||||
const useIndex = true;
|
const useIndex = true;
|
||||||
|
|
||||||
|
|
@ -35,7 +36,7 @@ module.exports = {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.set('Content-Type', 'text/html');
|
ctx.set('Content-Type', mime.contentType(path.extname(p)));
|
||||||
ctx.body = fs.createReadStream(p).on('error', (e)=>{ctx.onerror(e)}).pipe(PassThrough());
|
ctx.body = fs.createReadStream(p).on('error', (e)=>{ctx.onerror(e)}).pipe(PassThrough());
|
||||||
},
|
},
|
||||||
rules(ctx){
|
rules(ctx){
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue