diff --git a/README.md b/README.md
index 0f7db29..3bac0e2 100644
--- a/README.md
+++ b/README.md
@@ -1,18 +1,18 @@
-
+
@@ -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
diff --git a/index.js b/index.js
index 65a922e..524b044 100644
--- a/index.js
+++ b/index.js
@@ -1,4 +1,4 @@
const kirbe = exports;
-kirbe.Server = require( './model/KirbeServer' );
-kirbe.static = require( './model/middleware/static' );
\ No newline at end of file
+kirbe.Server = require('./model/KirbeServer.js');
+kirbe.Static = require('./model/middleware/KirbeStatic.js');
\ No newline at end of file
diff --git a/model/KirbeRequest.js b/model/KirbeRequest.js
index 5edfe68..ecde38b 100644
--- a/model/KirbeRequest.js
+++ b/model/KirbeRequest.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]; }
};
diff --git a/model/KirbeResponse.js b/model/KirbeResponse.js
index 9abb492..05c660f 100644
--- a/model/KirbeResponse.js
+++ b/model/KirbeResponse.js
@@ -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;
}
diff --git a/model/KirbeServer.js b/model/KirbeServer.js
index 2b2a0a2..9ee6d55 100644
--- a/model/KirbeServer.js
+++ b/model/KirbeServer.js
@@ -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;
}
};
diff --git a/model/middleware/static.js b/model/middleware/KirbeStatic.js
similarity index 57%
rename from model/middleware/static.js
rename to model/middleware/KirbeStatic.js
index 876b68d..50127ee 100644
--- a/model/middleware/static.js
+++ b/model/middleware/KirbeStatic.js
@@ -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();
+ }
+ });
+ }
+ });
+ };
};
diff --git a/package.json b/package.json
index 004370f..8463b9e 100644
--- a/package.json
+++ b/package.json
@@ -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
",
@@ -33,6 +33,6 @@
"lightweight"
],
"devDependencies": {
- "wumpfetch": "^0.0.5"
+ "wumpfetch": "^0.2.0"
}
}
diff --git a/test.js b/test.js
index 5f8b802..9c3a9b1 100644
--- a/test.js
+++ b/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' );
\ No newline at end of file
+app.listen(4040);
\ No newline at end of file
diff --git a/yarn.lock b/yarn.lock
index 7418dac..bc15291 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -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==