@blaasvaer/frmwrk 0.1.18 → 0.1.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/handle-request.js +3 -20
- package/index.js +15 -3
- package/package 2.json +30 -0
- package/package.json +1 -1
- package/router.js +23 -31
package/handle-request.js
CHANGED
|
@@ -5,8 +5,6 @@ const path = require('path');
|
|
|
5
5
|
const statics = require('serve-handler');
|
|
6
6
|
const { findRoute } = require('./router');
|
|
7
7
|
|
|
8
|
-
let headers = {};
|
|
9
|
-
|
|
10
8
|
const content_types = {
|
|
11
9
|
"text" : 'text/plain; charset=utf-8',
|
|
12
10
|
"html" : 'text/html; charset=utf-8',
|
|
@@ -51,6 +49,7 @@ async function handleRequest( req, res ) {
|
|
|
51
49
|
* Find route in Router based on url
|
|
52
50
|
*/
|
|
53
51
|
const route = findRoute( request_url );
|
|
52
|
+
|
|
54
53
|
/**
|
|
55
54
|
* Check for complete route
|
|
56
55
|
* @param {[type]} route [description]
|
|
@@ -60,23 +59,11 @@ async function handleRequest( req, res ) {
|
|
|
60
59
|
/**
|
|
61
60
|
* Create new controller from route
|
|
62
61
|
*/
|
|
63
|
-
const controller = new Controller( route, req, res );
|
|
62
|
+
const controller = new Controller( route, req, res, request_url );
|
|
64
63
|
|
|
65
64
|
let response_output = 'Nothing here …';
|
|
66
|
-
|
|
65
|
+
|
|
67
66
|
switch ( method ) {
|
|
68
|
-
case 'OPTIONS': // CORS request
|
|
69
|
-
headers = {
|
|
70
|
-
"Access-Control-Allow-Origin": "http://localhost",
|
|
71
|
-
"Access-Control-Allow-Methods": "OPTIONS, POST, GET, PUT, DELETE",
|
|
72
|
-
"Access-Control-Max-Age": 2592000, // 30 days // 86400, // 24 hours
|
|
73
|
-
"Access-Control-Allow-Credentials": false,
|
|
74
|
-
"Access-Control-Allow-Headers": "X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept"
|
|
75
|
-
/** add other headers as per requirement */
|
|
76
|
-
};
|
|
77
|
-
res.writeHead(204, headers);
|
|
78
|
-
res.end();
|
|
79
|
-
return;
|
|
80
67
|
case 'GET':
|
|
81
68
|
if ( ! controller.route.params ) {
|
|
82
69
|
/**
|
|
@@ -138,11 +125,7 @@ async function handleRequest( req, res ) {
|
|
|
138
125
|
|
|
139
126
|
// Cookies
|
|
140
127
|
res.setHeader( 'Set-Cookie','uuid=1234-1324-1234-1234-1234; Max-Age=3000; SameSite=lax; Secure' );
|
|
141
|
-
|
|
142
|
-
// CORS
|
|
143
128
|
res.setHeader( 'Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS' );
|
|
144
|
-
res.setHeader( 'Access-Control-Allow-Origin', 'http://localhost');
|
|
145
|
-
res.setHeader( 'Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
|
|
146
129
|
// res.setHeader( 'Content-Security-Policy', "default-src 'self'; img-src 'self';");
|
|
147
130
|
|
|
148
131
|
response_output = await controller.view( data );
|
package/index.js
CHANGED
|
@@ -60,12 +60,11 @@ FW = function ( config ) {
|
|
|
60
60
|
* @param {object} req The incomming request object
|
|
61
61
|
* @param {object} res The response object
|
|
62
62
|
*/
|
|
63
|
-
Controller = function Controller ( route, req, res ) {
|
|
64
|
-
// console.log("Controller in index (req.method):\n", req.method);
|
|
65
|
-
// console.log("Controller in index (req):\n", req);
|
|
63
|
+
Controller = function Controller ( route, req, res, url ) {
|
|
66
64
|
this.route = route;
|
|
67
65
|
this.req = req;
|
|
68
66
|
this.res = res;
|
|
67
|
+
this.url = url;
|
|
69
68
|
this.route.method = req.method;
|
|
70
69
|
|
|
71
70
|
/**
|
|
@@ -75,6 +74,19 @@ FW = function ( config ) {
|
|
|
75
74
|
route.data = data;
|
|
76
75
|
route.req = this.req;
|
|
77
76
|
route.res = this.res;
|
|
77
|
+
route.url = this.url;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Add search params to route
|
|
81
|
+
*/
|
|
82
|
+
let params = {};
|
|
83
|
+
|
|
84
|
+
for ( let pair of url.searchParams) {
|
|
85
|
+
params[pair[0]] = pair[1];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
route.searchParams = params;
|
|
89
|
+
|
|
78
90
|
let output = await route.action( route );
|
|
79
91
|
|
|
80
92
|
return output;
|
package/package 2.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@blaasvaer/frmwrk",
|
|
3
|
+
"version": "0.1.14",
|
|
4
|
+
"description": "My personal Node framework",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
|
+
"patch": "npm version patch --force && npm publish",
|
|
9
|
+
"major": "npm version major --force && npm publish",
|
|
10
|
+
"minor": "npm version minor --force && npm publish"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+ssh://git@bitbucket.org/blaasvaer/frmwrk.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"Node",
|
|
18
|
+
"Framework",
|
|
19
|
+
"Homebrew"
|
|
20
|
+
],
|
|
21
|
+
"author": "Sam Blåsvær",
|
|
22
|
+
"license": "ISC",
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://bitbucket.org/blaasvaer/frmwrk/issues"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://bitbucket.org/blaasvaer/frmwrk#readme",
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"serve-handler": "^6.1.3"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/package.json
CHANGED
package/router.js
CHANGED
|
@@ -30,55 +30,47 @@ global.ROUTE = function ( route, action, params = null ) {
|
|
|
30
30
|
global.routes.push( r );
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
-
function Router ( req, url ) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
// function Router ( req, url ) {
|
|
34
|
+
// const method = req.method;
|
|
35
|
+
// // console.log("Router url:", url);
|
|
36
|
+
// // Split url into array
|
|
37
|
+
// let url_array = url.pathname.split('/');
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
39
|
+
// // Remove first empty element
|
|
40
|
+
// url_array.shift();
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
// // Set controller, resource, resource_id, search, searchParams
|
|
43
|
+
// this.controller = url_array[0];
|
|
44
|
+
// this.resource = url_array[1];
|
|
45
|
+
// this.resource_id = url_array[2];
|
|
46
|
+
// this.search = url_array[3];
|
|
47
|
+
// this.searchParams = url_array[4];
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
// // console.log("referer", referer);
|
|
50
|
+
// // console.log("url", url);
|
|
51
|
+
// // console.log("this.controller", this.controller);
|
|
52
|
+
// // console.log("this.resource", this.resource);
|
|
53
|
+
// // console.log("this.resource_id", this.resource_id);
|
|
54
|
+
// // console.log("this.search", this.search);
|
|
55
|
+
// // console.log("this.searchParams", this.searchParams);
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
};
|
|
57
|
+
// return this;
|
|
58
|
+
// };
|
|
59
59
|
|
|
60
60
|
function findRoute ( url ) {
|
|
61
61
|
let pathname = url.pathname;
|
|
62
|
-
// console.log( "ROUTE, url", url );
|
|
63
62
|
|
|
64
|
-
// console.log("THIS", this);
|
|
65
|
-
// Check if route exist
|
|
66
63
|
let route = global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes( pathname ) );
|
|
67
64
|
|
|
68
65
|
if ( route ) {
|
|
69
|
-
// console.log("Route defined url:", url);
|
|
70
|
-
// console.log("Route defined route:", route);
|
|
71
|
-
// Execute action for route
|
|
72
66
|
return route;
|
|
73
67
|
} else {
|
|
74
|
-
// console.log("Route NOT defined", url);
|
|
75
68
|
const url_array = pathname.split('/').filter( e => e );
|
|
76
69
|
|
|
77
70
|
// Check if route is an API call
|
|
78
71
|
let isAPICall = pathname.split('/')[1] === 'api' ? true : false;
|
|
79
72
|
|
|
80
73
|
if ( isAPICall ) {
|
|
81
|
-
// console.log("Route NOT defined (API)", url, url.split('/')[1]);
|
|
82
74
|
/**
|
|
83
75
|
* If checking for UUID use:
|
|
84
76
|
* UUID v1:
|
|
@@ -122,6 +114,6 @@ function findRoute ( url ) {
|
|
|
122
114
|
}
|
|
123
115
|
|
|
124
116
|
module.exports = {
|
|
125
|
-
Router,
|
|
117
|
+
// Router,
|
|
126
118
|
findRoute
|
|
127
119
|
}
|