mirror of
https://github.com/Wessel/kirbe.git
synced 2026-06-08 14:10:13 +02:00
Improvements to middleware
This commit is contained in:
12
README.md
12
README.md
@@ -30,7 +30,7 @@ $ npm i kirbe # Install w/ NPM
|
|||||||
#### Start a HTTP(s) server on port 8080 and add some routes
|
#### Start a HTTP(s) server on port 8080 and add some routes
|
||||||
```js
|
```js
|
||||||
const kirbe = require( 'kirbe' ); // Define kirbe
|
const kirbe = require( 'kirbe' ); // Define kirbe
|
||||||
const app = new kirbe(); // Make your kirbe client
|
const app = new kirbe(); // Make your kirbe instance
|
||||||
|
|
||||||
app.route( '/bear', 'GET', ( req, res ) => res.status( 200 ).body({ 'bear': 'cop' }) );
|
app.route( '/bear', 'GET', ( req, res ) => res.status( 200 ).body({ 'bear': 'cop' }) );
|
||||||
app.route( ( req, res ) => res.status( 404 ).body( 'Error: Content not found!' ).end() );
|
app.route( ( req, res ) => res.status( 404 ).body( 'Error: Content not found!' ).end() );
|
||||||
@@ -46,19 +46,21 @@ const https = require( 'https' ); // This should be at the top of your code
|
|||||||
https.createServer( app.externalHandler ).listen( 8080 );
|
https.createServer( app.externalHandler ).listen( 8080 );
|
||||||
```
|
```
|
||||||
|
|
||||||
## Default extensions ( [/extensions](extensions) )
|
## Default extensions ( [/model/middleware](model/middleware) )
|
||||||
#### Static
|
#### Static
|
||||||
> Host static files on your website
|
Host static files on your website
|
||||||
|
|
||||||
##### Usage
|
##### Usage
|
||||||
```js
|
```js
|
||||||
const path = require( 'path' ); // Define path
|
const path = require( 'path' ); // Define path
|
||||||
const kirbe = require( 'kirbe' ); // Define kirbe
|
const kirbe = require( 'kirbe' ); // Define kirbe
|
||||||
const app = new kirbe(); // Make your kirbe client
|
const app = new kirbe(); // Make your kirbe instance
|
||||||
|
|
||||||
app.use( kirbe.static( path.join( __dirname, 'static' ) ) );
|
app.use( kirbe.static( path.join( __dirname, 'static' ) ) );
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Creating your own
|
||||||
|
If you want to add your own extension/middleware, you can either create your own module or add it to the [/model/middleware](model/middleware) and then create a pull request!
|
||||||
|
|
||||||
### Why use kirbe?
|
### Why use kirbe?
|
||||||
Kirbe is a lightweight and fast HTTP server library, especially comparing to express and connect which are around 1mb in size with multiple dependencies. If you want any features that aren't inside of Kirbe yet, you can open an issue or pull request.
|
Kirbe is a lightweight and fast HTTP server library, especially comparing to express and connect which are around 1mb in size with multiple dependencies. If you want any features that aren't inside of Kirbe yet, you can open an issue or pull request.
|
||||||
|
|
||||||
|
|||||||
27
fake_node_modules/Collection.js
generated
Normal file
27
fake_node_modules/Collection.js
generated
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
class Collection extends Map {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
filter(callback) {
|
||||||
|
let result = [];
|
||||||
|
const all = Array.from(this.values());
|
||||||
|
for (let i = 0; i < all.length; i++) {
|
||||||
|
if (callback(all[i])) result.push(all[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
map(callback) {
|
||||||
|
const values = Array.from(this.values());
|
||||||
|
let result = [];
|
||||||
|
for (let i = 0; i < values.length; i++) {
|
||||||
|
result.push(callback(values[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = Collection;
|
||||||
4
index.js
4
index.js
@@ -1,4 +1,4 @@
|
|||||||
const kirbe = exports;
|
const kirbe = exports;
|
||||||
|
|
||||||
module.exports = require( './model/KirbeServer' );
|
kirbe.Server = require( './model/KirbeServer' );
|
||||||
kirbe.static = require( './extensions/static' );
|
kirbe.static = require( './model/middleware/static' );
|
||||||
@@ -4,6 +4,8 @@ const { createServer } = require( 'http' );
|
|||||||
const KirbeRequest = require( join( __dirname, 'KirbeRequest.js' ) );
|
const KirbeRequest = require( join( __dirname, 'KirbeRequest.js' ) );
|
||||||
const KirbeResponse = require( join( __dirname, 'KirbeResponse.js' ) );
|
const KirbeResponse = require( join( __dirname, 'KirbeResponse.js' ) );
|
||||||
|
|
||||||
|
const Collection = require( '../fake_node_modules/Collection' );
|
||||||
|
|
||||||
const methods = [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH' ];
|
const methods = [ 'GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH' ];
|
||||||
const isUrl = ( c ) => (typeof c === 'string' && !methods.includes( c ) ) || c instanceof RegExp;
|
const isUrl = ( c ) => (typeof c === 'string' && !methods.includes( c ) ) || c instanceof RegExp;
|
||||||
|
|
||||||
@@ -12,13 +14,23 @@ module.exports = class KirbeServer {
|
|||||||
this.internalServer = createServer( ( req, res ) => this.handler.apply( this, [ req, res ] ) );
|
this.internalServer = createServer( ( req, res ) => this.handler.apply( this, [ req, res ] ) );
|
||||||
this.externalHandler = ( req, res ) => this.handler( req, res );
|
this.externalHandler = ( req, res ) => this.handler( req, res );
|
||||||
|
|
||||||
|
this.stack = new Collection();
|
||||||
this.routes = [];
|
this.routes = [];
|
||||||
this.extensions = [];
|
this.extensions = [];
|
||||||
|
|
||||||
methods.forEach( ( v ) => this[ v.toLowerCase() ] = ( a, b ) => this.route( v, a, b ) );
|
methods.forEach( ( v ) => this[ v.toLowerCase() ] = ( a, b ) => this.route( v, a, b ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
use( extension ) { this.extensions.push( extension ); }
|
use( middleware ) {
|
||||||
|
const o = typeof middleware === 'object';
|
||||||
|
const m = {
|
||||||
|
name : this.stack.size,
|
||||||
|
args : o && typeof Array.isArray( o.args ) ? o.args : [],
|
||||||
|
function: o && typeof o.function === 'function' ? o.function : middleware
|
||||||
|
};
|
||||||
|
|
||||||
|
this.stack.set( m.name, m );
|
||||||
|
}
|
||||||
|
|
||||||
route( a, b, c ) {
|
route( a, b, c ) {
|
||||||
this.routes.push({
|
this.routes.push({
|
||||||
@@ -53,15 +65,15 @@ module.exports = class KirbeServer {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let currentExt = 0;
|
let currentMiddleware = 0;
|
||||||
const nextExt = () => {
|
const renderMiddleware = () => {
|
||||||
if ( this.extensions.length >= currentExt + 1 ) {
|
if( this.stack.size >= currentMiddleware +1 ) {
|
||||||
currentExt++;
|
currentMiddleware++;
|
||||||
this.extensions[ currentExt -1 ]( request, response, nextExt );
|
const middleware = this.stack.get( currentMiddleware - 1 );
|
||||||
|
middleware.function( request, response, renderMiddleware );
|
||||||
}else start();
|
}else start();
|
||||||
};
|
};
|
||||||
|
renderMiddleware();
|
||||||
nextExt();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,8 @@
|
|||||||
"index.js",
|
"index.js",
|
||||||
"model",
|
"model",
|
||||||
"extensions",
|
"extensions",
|
||||||
"LICENSE"
|
"LICENSE",
|
||||||
|
"fake_node_modules"
|
||||||
],
|
],
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://www.github.com/PassTheWessel/kirbe/issues"
|
"url": "https://www.github.com/PassTheWessel/kirbe/issues"
|
||||||
@@ -32,6 +33,6 @@
|
|||||||
"lightweight"
|
"lightweight"
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"wumpfetch": "^0.0.4"
|
"wumpfetch": "^0.0.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
4
test.js
4
test.js
@@ -1,9 +1,9 @@
|
|||||||
const w = require( 'wumpfetch' );
|
const w = require( 'wumpfetch' );
|
||||||
const Kirbe = require( './index' );
|
const Kirbe = require( './index' );
|
||||||
|
|
||||||
const app = new Kirbe();
|
const app = new Kirbe.Server();
|
||||||
|
|
||||||
app.use( ( req, res, next ) => req.url === '/testExtension' ? res.status( 200 ).end() : next() );
|
app.use( ( req, res, next ) => req.url === '/middleware' ? res.status( 200 ).end() : next() );
|
||||||
|
|
||||||
app.post( '/parse', ( req, res ) => res.body({ 'sent': req.json() }).end() );
|
app.post( '/parse', ( req, res ) => res.body({ 'sent': req.json() }).end() );
|
||||||
app.route( 'GET', '/statusMsg', ( req, res ) => res.status( 200, 'kirbe won' ).end() );
|
app.route( 'GET', '/statusMsg', ( req, res ) => res.status( 200, 'kirbe won' ).end() );
|
||||||
|
|||||||
8
yarn.lock
Normal file
8
yarn.lock
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||||
|
# yarn lockfile v1
|
||||||
|
|
||||||
|
|
||||||
|
wumpfetch@^0.0.5:
|
||||||
|
version "0.0.5"
|
||||||
|
resolved "https://registry.yarnpkg.com/wumpfetch/-/wumpfetch-0.0.5.tgz#c9afe264eb4190b9e4ecb5839c98e0a2c9ecf143"
|
||||||
|
integrity sha512-FqLFJCPX7A61egdlGQ6yK9eTWkHP1+n2+BJoW6S+1foRzqiV9sr2b9Tdw+w/jnW7MqcqZ+j1SAmcKfVxSYsEgA==
|
||||||
Reference in New Issue
Block a user