mirror of
https://github.com/danbulant/koa-plugins
synced 2026-05-20 20:58:58 +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:
|
||||
|
||||
| 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 |
|
||||
| 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:
|
||||
|
||||
| exec | Function to call when request rules matches |
|
||||
| ---- | ---- |
|
||||
| rules | *optional*, same as in object plugin |
|
||||
|
||||
* `exec` Function to call when request rules matches
|
||||
* `rules` *optional*, same as in object plugin
|
||||
|
||||
### Rules
|
||||
|
||||
Following rules can be used
|
||||
|
||||
| Rule | Type | Description |
|
||||
| ---- | ---- | ----------- |
|
||||
| path | String or String[] | If path matches **exactly**. |
|
||||
| pathReg | Regex | Regex to be checked against (must be instance of regex) |
|
||||
| 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",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-01paPWYgLrkqAyrlDorC1uDwl2p3qZT7yl806vW7DvDoxwXi46jsjFbg+WdwotBIk6/MbEhO/dh5aZ5sNj/dWQ==",
|
||||
"_location": "/mime-types",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"type": "tag",
|
||||
"registry": true,
|
||||
"raw": "mime-types@~2.1.24",
|
||||
"raw": "mime-types",
|
||||
"name": "mime-types",
|
||||
"escapedName": "mime-types",
|
||||
"rawSpec": "~2.1.24",
|
||||
"rawSpec": "",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "~2.1.24"
|
||||
"fetchSpec": "latest"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"#USER",
|
||||
"/",
|
||||
"/accepts",
|
||||
"/cache-content-type",
|
||||
"/type-is"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.26.tgz",
|
||||
"_shasum": "9c921fc09b7e149a65dfdc0da4d20997200b0a06",
|
||||
"_spec": "mime-types@~2.1.24",
|
||||
"_where": "/home/dan/Documents/node-fw/node_modules/accepts",
|
||||
"_spec": "mime-types",
|
||||
"_where": "/home/dan/Documents/node-fw",
|
||||
"bugs": {
|
||||
"url": "https://github.com/jshttp/mime-types/issues"
|
||||
},
|
||||
|
|
|
|||
10
package.json
10
package.json
|
|
@ -1,17 +1,21 @@
|
|||
{
|
||||
"name": "node-fw",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"description": "Plugin-based webserver for node",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"author": "Daniel Bulant",
|
||||
"repository": {
|
||||
"url": "https://github.com/danbulant/koa-plugins"
|
||||
},
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"chalk": "^3.0.0",
|
||||
"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 path = require("path")
|
||||
const PassThrough = require('stream').PassThrough;
|
||||
const mime = require('mime-types');
|
||||
|
||||
const useIndex = true;
|
||||
|
||||
|
|
@ -15,7 +16,7 @@ module.exports = {
|
|||
|
||||
if(ctx.request.url.split("/")[ctx.request.url.split("/").length - 1] == "" && useIndex){
|
||||
p = path.join(p, "index.html");
|
||||
|
||||
|
||||
if(!fs.existsSync(p)){
|
||||
ctx.body = "Forbidden";
|
||||
ctx.status = 403;
|
||||
|
|
@ -35,7 +36,7 @@ module.exports = {
|
|||
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());
|
||||
},
|
||||
rules(ctx){
|
||||
|
|
|
|||
Loading…
Reference in a new issue