mirror of
https://github.com/Wessel/kirbe.git
synced 2026-07-15 21:13:38 +02:00
28
README.md
28
README.md
@@ -1,18 +1,18 @@
|
||||
<div align="center">
|
||||
<div align='center'>
|
||||
<br />
|
||||
<p>
|
||||
<img src="media/kirb.gif" /><h1 style="font-size:107px;">Kirbe</h1>
|
||||
<img src='media/kirb.gif' /><h1 style='font-size:107px;'>Kirbe</h1>
|
||||
</p>
|
||||
<br />
|
||||
<p>
|
||||
<a href="https://packagephobia.now.sh/result?p=kirbe"><img src="https://packagephobia.now.sh/badge?p=kirbe" alt="Install Size" /></a>
|
||||
<a href="https://discord.gg/SV7DAE9"><img src="https://discordapp.com/api/guilds/107131083958538240/embed.png" alt="Discord" /></a>
|
||||
<a href="https://www.npmjs.com/package/kirbe"><img src="https://img.shields.io/npm/v/kirbe.svg?maxAge=3600" alt="NPM version" /></a>
|
||||
<a href="https://www.npmjs.com/package/kirbe"><img src="https://img.shields.io/npm/dt/kirbe.svg?maxAge=3600" alt="NPM version" /></a>
|
||||
<a href="https://www.patreon.com/wessel"><img src="https://img.shields.io/badge/donate-patreon-F96854.svg" alt="Patreon" /></a>
|
||||
<a href='https://packagephobia.now.sh/result?p=kirbe'><img src='https://packagephobia.now.sh/badge?p=kirbe' alt='Install Size' /></a>
|
||||
<a href='https://discord.gg/SV7DAE9'><img src='https://discordapp.com/api/guilds/107131083958538240/embed.png' alt='Discord' /></a>
|
||||
<a href='https://www.npmjs.com/package/kirbe'><img src='https://img.shields.io/npm/v/kirbe.svg?maxAge=3600' alt='NPM version' /></a>
|
||||
<a href='https://www.npmjs.com/package/kirbe'><img src='https://img.shields.io/npm/dt/kirbe.svg?maxAge=3600' alt='NPM version' /></a>
|
||||
<a href='https://www.patreon.com/wessel'><img src='https://img.shields.io/badge/donate-patreon-F96854.svg' alt='Patreon' /></a>
|
||||
</p>
|
||||
<p>
|
||||
<a href="https://nodei.co/npm/kirbe/"><img src="https://nodei.co/npm/kirbe.png?downloads=true&stars=true" alt="npm installnfo" /></a>
|
||||
<a href='https://nodei.co/npm/kirbe/'><img src='https://nodei.co/npm/kirbe.png?downloads=true&stars=true' alt='npm installnfo' /></a>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@@ -33,11 +33,11 @@ const kirbe = require('kirbe'); // Define kirbe
|
||||
|
||||
const app = new kirbe.Server(); // 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() );
|
||||
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.get('/kirb', (req, res) => {
|
||||
res.writeHead( 201, { 'test': 'hi' });
|
||||
res.end({ 'key': 'hi' });
|
||||
res.writeHead(201, {'test': 'hi'});
|
||||
res.end({'key': 'hi'});
|
||||
});
|
||||
|
||||
// HTTP
|
||||
@@ -55,9 +55,9 @@ Host static files on your website
|
||||
const path = require('path'); // Define path
|
||||
const kirbe = require('kirbe'); // Define kirbe
|
||||
|
||||
const app = new kirbe(); // Make your kirbe instance
|
||||
const app = new kirbe.Server(); // Make your kirbe instance
|
||||
|
||||
app.use(kirbe.static(path.join(__dirname, 'static')));
|
||||
app.use(kirbe.Static(path.join(__dirname, 'static')));
|
||||
```
|
||||
|
||||
### Creating your own
|
||||
|
||||
4
index.js
4
index.js
@@ -1,4 +1,4 @@
|
||||
const kirbe = exports;
|
||||
|
||||
kirbe.Server = require( './model/KirbeServer' );
|
||||
kirbe.static = require( './model/middleware/static' );
|
||||
kirbe.Server = require('./model/KirbeServer.js');
|
||||
kirbe.Static = require('./model/middleware/KirbeStatic.js');
|
||||
@@ -2,15 +2,15 @@ const { parse } = require('url');
|
||||
|
||||
module.exports = class KirbeRequest {
|
||||
constructor(req, body) {
|
||||
this.url = req.url;
|
||||
this.req = req;
|
||||
this.body = body;
|
||||
this.from = req.connection.remoteAddress;
|
||||
this.method = req.method;
|
||||
this.headers = req.headers;
|
||||
this.parsedUrl = parse(this.url, true);
|
||||
this.url = req.url;
|
||||
this.req = req;
|
||||
this.body = body;
|
||||
this.from = req.connection.remoteAddress;
|
||||
this.method = req.method;
|
||||
this.headers = req.headers;
|
||||
this.parsedUrl = parse(this.url, true);
|
||||
}
|
||||
|
||||
json() { return JSON.parse(this.body); }
|
||||
query (name) { return this.parsedUrl.query[name]; }
|
||||
json() { return JSON.parse(this.body); }
|
||||
query (name) { return this.parsedUrl.query[name]; }
|
||||
};
|
||||
|
||||
@@ -2,7 +2,7 @@ module.exports = class KirbeResponse {
|
||||
constructor(res) {
|
||||
this.coreRes = res;
|
||||
this.headers = {};
|
||||
|
||||
|
||||
this.statusCode = 200;
|
||||
this.statusMessage = null;
|
||||
|
||||
@@ -11,24 +11,20 @@ module.exports = class KirbeResponse {
|
||||
|
||||
body(body) {
|
||||
if (typeof body === 'object' && !Buffer.isBuffer(body)) {
|
||||
if (!this.headers['content-type']) this.headers['content-type'] = 'application/json';
|
||||
this.data = JSON.stringify(body);
|
||||
if (!this.headers['content-type']) this.headers['content-type'] = 'application/json'; this.data = JSON.stringify(body);
|
||||
} else this.data = body;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
header(a, b) {
|
||||
if (typeof a === 'object') Object.keys(a).forEach((v) => this.headers[v.toLowerCase()] = a[v]);
|
||||
else this.headers[a.toLowerCase()] = b;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
status(code, message) {
|
||||
this.statusCode = code;
|
||||
this.statusMessage = message;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
const { join } = require('path');
|
||||
const { createServer } = require('http');
|
||||
|
||||
const KirbeRequest = require(join( __dirname, 'KirbeRequest.js'));
|
||||
const KirbeResponse = require(join( __dirname, 'KirbeResponse.js'));
|
||||
const KirbeRequest = require('./KirbeRequest.js');
|
||||
const KirbeResponse = require('./KirbeResponse.js');
|
||||
|
||||
const Collection = require( '../fake_node_modules/Collection' );
|
||||
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;
|
||||
@@ -62,7 +61,7 @@ module.exports = class KirbeServer {
|
||||
}
|
||||
|
||||
current.handler( request, response );
|
||||
break;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -20,27 +20,24 @@ module.exports = (baseDir, indexFile) => {
|
||||
|
||||
if (stats.isFile()) {
|
||||
stats.mtime.setMilliseconds(0);
|
||||
if (stats.mtime <= new Date(req.headers['if-modified-since'])) res.status(304).end();
|
||||
else createReadStream(filePath).pipe(res.status(200).coreRes);
|
||||
if (stats.mtime <= new Date(req.headers['if-modified-since'])) res.status(304).end();
|
||||
else createReadStream(filePath).pipe(res.status(200).coreRes);
|
||||
} else {
|
||||
if (req.parsedUrl.pathname.charAt(req.parsedUrl.pathname.length -1 ) !== '/') {
|
||||
res.status(302).header({'Location': `${req.parsedUrl.pathname}/`}).end();
|
||||
return;
|
||||
}
|
||||
|
||||
requestedPath = join(filePath, indexFile);
|
||||
requestedExt = extname(requestedPath);
|
||||
if (req.parsedUrl.pathname.charAt(req.parsedUrl.pathname.length -1 ) !== '/') return res.status(302).header({'Location': `${req.parsedUrl.pathname}/`}).end();
|
||||
|
||||
requestedPath = join(filePath, indexFile);
|
||||
requestedExt = extname(requestedPath);
|
||||
|
||||
readFile(requestedPath, (err, data) => {
|
||||
if (err) next();
|
||||
else {
|
||||
res.body(data).status(200).header({
|
||||
'Content-Type': (mimes.hasOwnProperty(requestedExt) ? mimes[requestedExt] : 'application/octet-stream'),
|
||||
'Last-Modified': stats.mtime.toString()
|
||||
}).end();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
readFile(requestedPath, (err, data) => {
|
||||
if (err) next();
|
||||
else {
|
||||
res.body(data).status(200).header({
|
||||
'Content-Type': (mimes.hasOwnProperty(requestedExt) ? mimes[requestedExt] : 'application/octet-stream'),
|
||||
'Last-Modified': stats.mtime.toString()
|
||||
}).end();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "kirbe",
|
||||
"version": "0.0.4",
|
||||
"version": "0.0.5",
|
||||
"description": "👻 A powerful and lightweight Node.js HTTP server library",
|
||||
"main": "index.js",
|
||||
"author": "Wessel \"wesselgame\" T <discord@go2it.eu>",
|
||||
@@ -33,6 +33,6 @@
|
||||
"lightweight"
|
||||
],
|
||||
"devDependencies": {
|
||||
"wumpfetch": "^0.0.5"
|
||||
"wumpfetch": "^0.2.0"
|
||||
}
|
||||
}
|
||||
|
||||
36
test.js
36
test.js
@@ -1,28 +1,28 @@
|
||||
const w = require( 'wumpfetch' );
|
||||
const Kirbe = require( './index' );
|
||||
const w = require('wumpfetch');
|
||||
const Kirbe = require('./index');
|
||||
|
||||
const app = new Kirbe.Server();
|
||||
|
||||
app.use( ( req, res, next ) => req.url === '/middleware' ? 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() );
|
||||
app.route( 'POST', ( req, res ) => res.body( 'Gotta catch em all!' ).end() );
|
||||
app.route( '/compatibility', ( req, res ) => {
|
||||
res.writeHead( 201, { 'test': 'hi' });
|
||||
res.end({ 'key': 'hi' });
|
||||
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('POST', (req, res) => res.body('Gotta catch em all!').end());
|
||||
app.route('/compatibility', (req, res) => {
|
||||
res.writeHead(201, {'test': 'hi'});
|
||||
res.end({'key': 'hi'});
|
||||
});
|
||||
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());
|
||||
|
||||
;( async() => {
|
||||
;(async() => {
|
||||
const res = [];
|
||||
res.push( await w( 'http://127.0.0.1:4040/bear' ).send() );
|
||||
res.push( await w( 'http://127.0.0.1:4040/statusMsg' ).send() );
|
||||
res.push( await w( 'http://127.0.0.1:4040/compatibility' ).send() );
|
||||
res.push( await w( 'http://127.0.0.1:4040/testExtension' ).send() );
|
||||
res.push( await w( 'http://127.0.0.1:4040/parse', 'POST' ).body({ 'hello': 123 }).send() );
|
||||
res.push(await w('http://127.0.0.1:4040/bear').send());
|
||||
res.push(await w('http://127.0.0.1:4040/statusMsg').send());
|
||||
res.push(await w('http://127.0.0.1:4040/compatibility').send());
|
||||
res.push(await w('http://127.0.0.1:4040/testExtension').send());
|
||||
res.push(await w('http://127.0.0.1:4040/parse', 'POST').body({'hello': 123}).send());
|
||||
|
||||
res.forEach( ( v, i ) => console.log( `${i}: ${v.statusCode}` ) );
|
||||
res.forEach((v, i) => console.log( `${i}: ${v.statusCode}`));
|
||||
})();
|
||||
|
||||
app.listen( 4040, '127.0.0.1' );
|
||||
app.listen(4040);
|
||||
@@ -2,7 +2,7 @@
|
||||
# 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==
|
||||
wumpfetch@^0.2.0:
|
||||
version "0.2.0"
|
||||
resolved "https://registry.yarnpkg.com/wumpfetch/-/wumpfetch-0.2.0.tgz#fb3aba196c9b4dfade3f29f3612941ee57426902"
|
||||
integrity sha512-PfsEQ+DJiicHnjkg0NQW0e927CSU1MZtgQRVQZKdD6r5o0wb/lXAVN6pDEZ2zVlcxQ9PoEhRHbGDx91tfcG5Ng==
|
||||
|
||||
Reference in New Issue
Block a user