mirror of
https://github.com/Wessel/kirbe.git
synced 2026-06-05 23:25:45 +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
|
||||
```js
|
||||
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( ( 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 );
|
||||
```
|
||||
|
||||
## Default extensions ( [/extensions](extensions) )
|
||||
## Default extensions ( [/model/middleware](model/middleware) )
|
||||
#### Static
|
||||
> Host static files on your website
|
||||
|
||||
Host static files on your website
|
||||
##### Usage
|
||||
```js
|
||||
const path = require( 'path' ); // Define path
|
||||
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' ) ) );
|
||||
```
|
||||
|
||||
### 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?
|
||||
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;
|
||||
|
||||
module.exports = require( './model/KirbeServer' );
|
||||
kirbe.static = require( './extensions/static' );
|
||||
kirbe.Server = require( './model/KirbeServer' );
|
||||
kirbe.static = require( './model/middleware/static' );
|
||||
@@ -4,6 +4,8 @@ const { createServer } = require( 'http' );
|
||||
const KirbeRequest = require( join( __dirname, 'KirbeRequest.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 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.externalHandler = ( req, res ) => this.handler( req, res );
|
||||
|
||||
this.stack = new Collection();
|
||||
this.routes = [];
|
||||
this.extensions = [];
|
||||
|
||||
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 ) {
|
||||
this.routes.push({
|
||||
@@ -53,16 +65,16 @@ module.exports = class KirbeServer {
|
||||
}
|
||||
};
|
||||
|
||||
let currentExt = 0;
|
||||
const nextExt = () => {
|
||||
if ( this.extensions.length >= currentExt + 1 ) {
|
||||
currentExt++;
|
||||
this.extensions[ currentExt -1 ]( request, response, nextExt );
|
||||
} else start();
|
||||
let currentMiddleware = 0;
|
||||
const renderMiddleware = () => {
|
||||
if( this.stack.size >= currentMiddleware +1 ) {
|
||||
currentMiddleware++;
|
||||
const middleware = this.stack.get( currentMiddleware - 1 );
|
||||
middleware.function( request, response, renderMiddleware );
|
||||
}else start();
|
||||
};
|
||||
|
||||
nextExt();
|
||||
});
|
||||
renderMiddleware();
|
||||
});
|
||||
}
|
||||
|
||||
listen( a, b, c ) {
|
||||
|
||||
@@ -10,7 +10,8 @@
|
||||
"index.js",
|
||||
"model",
|
||||
"extensions",
|
||||
"LICENSE"
|
||||
"LICENSE",
|
||||
"fake_node_modules"
|
||||
],
|
||||
"bugs": {
|
||||
"url": "https://www.github.com/PassTheWessel/kirbe/issues"
|
||||
@@ -32,6 +33,6 @@
|
||||
"lightweight"
|
||||
],
|
||||
"devDependencies": {
|
||||
"wumpfetch": "^0.0.4"
|
||||
"wumpfetch": "^0.0.5"
|
||||
}
|
||||
}
|
||||
|
||||
4
test.js
4
test.js
@@ -1,9 +1,9 @@
|
||||
const w = require( 'wumpfetch' );
|
||||
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.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