From 9f1cb532e5a8835cdda6cea50c33bbf28e03220d Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Sun, 1 Mar 2020 19:02:49 +0100 Subject: [PATCH] Fix readme tables & add automatic mime types to static --- README.md | 11 +++++++---- node_modules/mime-types/package.json | 16 +++++++++------- package.json | 10 +++++++--- plugins/static/index.js | 5 +++-- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 9a56b65..3c99660 100644 --- a/README.md +++ b/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). 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 | diff --git a/node_modules/mime-types/package.json b/node_modules/mime-types/package.json index 0ebbf09..1d4312b 100644 --- a/node_modules/mime-types/package.json +++ b/node_modules/mime-types/package.json @@ -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" }, diff --git a/package.json b/package.json index 57a8d7f..6314dbe 100644 --- a/package.json +++ b/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" } } diff --git a/plugins/static/index.js b/plugins/static/index.js index 25a93fa..374d96f 100644 --- a/plugins/static/index.js +++ b/plugins/static/index.js @@ -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){