@blaasvaer/frmwrk 0.1.14 → 0.1.15
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 +71 -49
- package/index.js +16 -1
- package/models.js +0 -1
- package/package 2.json +30 -0
- package/package.json +1 -1
- package/router.js +17 -9
package/handle-request.js
CHANGED
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const path = require('path');
|
|
5
5
|
const statics = require('serve-handler');
|
|
6
|
-
const
|
|
7
|
-
const { findRoute, Router } = require('./router');
|
|
6
|
+
const { findRoute } = require('./router');
|
|
8
7
|
|
|
9
8
|
const content_types = {
|
|
10
9
|
"text" : 'text/plain; charset=utf-8',
|
|
@@ -19,23 +18,25 @@ const content_types = {
|
|
|
19
18
|
* @return {Object} Return the response
|
|
20
19
|
*/
|
|
21
20
|
async function handleRequest( req, res ) {
|
|
22
|
-
|
|
23
|
-
const request_url = new URL( req.url, `http://${req.headers.host}`);
|
|
21
|
+
let method = req.method;
|
|
24
22
|
|
|
23
|
+
// console.log("handleRequest, req", req );
|
|
24
|
+
const request_url = new URL( req.url, `http://${req.headers.host}`);
|
|
25
|
+
// console.log("handleRequest, request_url", request_url);
|
|
25
26
|
/**
|
|
26
27
|
* Set default status and header
|
|
27
28
|
* @type {Number}
|
|
28
29
|
*/
|
|
29
30
|
let status_code = 200;
|
|
30
31
|
let content_type = 'html';
|
|
31
|
-
|
|
32
|
+
|
|
32
33
|
// Prevent errors from missing favicon
|
|
33
34
|
if (request_url.pathname === '/favicon.ico') {
|
|
34
35
|
// TODO: Set theme dynamically
|
|
35
|
-
var favicon = path.join('
|
|
36
|
+
var favicon = path.join('assets/favicon', 'favicon.ico');
|
|
36
37
|
res.statusCode = 200;
|
|
37
38
|
res.setHeader('Content-Type', 'image/x-icon');
|
|
38
|
-
|
|
39
|
+
|
|
39
40
|
if ( fs.existsSync( favicon ) ) {
|
|
40
41
|
fs.createReadStream( favicon ).pipe( res );
|
|
41
42
|
return;
|
|
@@ -43,13 +44,11 @@ async function handleRequest( req, res ) {
|
|
|
43
44
|
return;
|
|
44
45
|
}
|
|
45
46
|
}
|
|
46
|
-
|
|
47
|
+
|
|
47
48
|
/**
|
|
48
49
|
* Find route in Router based on url
|
|
49
50
|
*/
|
|
50
|
-
|
|
51
|
-
const route = findRoute( request_url.pathname );
|
|
52
|
-
// console.log("route", route);
|
|
51
|
+
const route = findRoute( request_url );
|
|
53
52
|
/**
|
|
54
53
|
* Check for complete route
|
|
55
54
|
* @param {[type]} route [description]
|
|
@@ -62,48 +61,71 @@ async function handleRequest( req, res ) {
|
|
|
62
61
|
const controller = new Controller( route, req, res );
|
|
63
62
|
|
|
64
63
|
let response_output = 'Nothing here …';
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
64
|
+
|
|
65
|
+
switch ( method ) {
|
|
66
|
+
case 'GET':
|
|
67
|
+
|
|
68
|
+
if ( ! controller.route.params ) {
|
|
69
|
+
/**
|
|
70
|
+
* If the route has no parameters, then just run the route method and serve whatever it may …
|
|
71
|
+
*/
|
|
72
|
+
response_output = await controller.view();
|
|
73
|
+
} else {
|
|
74
|
+
/**
|
|
75
|
+
* Authorization
|
|
76
|
+
*/
|
|
77
|
+
if ( Authorize( controller.route ) ) {
|
|
78
|
+
response_output = await controller.view();
|
|
79
|
+
} else {
|
|
80
|
+
/**
|
|
81
|
+
* Redirect the user to login if auth fails
|
|
82
|
+
*/
|
|
83
|
+
if ( controller.route.params.access.redirect ) {
|
|
84
|
+
status_code = 302;
|
|
85
|
+
res.setHeader( 'Location', controller.route.params.access.redirect );
|
|
86
|
+
}
|
|
87
|
+
// console.log("NO USER", controller.route.params.redirect);
|
|
88
|
+
// response_output = redirect.action.call();
|
|
89
|
+
// route.params.redirect.call();
|
|
90
|
+
// response_output = 'Access denied';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if ( controller.route.params.type ) {
|
|
94
|
+
content_type = controller.route.params.type;
|
|
95
|
+
}
|
|
84
96
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
97
|
+
|
|
98
|
+
res.statusCode = status_code;
|
|
99
|
+
res.setHeader( 'Content-Type', content_types[content_type] );
|
|
100
|
+
|
|
101
|
+
// Cookies
|
|
102
|
+
res.setHeader('Set-Cookie','uuid=1234-1324-1234-1234-1234; Max-Age=3000; SameSite=lax; Secure');
|
|
103
|
+
// Set a same-site cookie for first-party contexts
|
|
104
|
+
// res.cookie('cookie1', 'value1', { sameSite: 'lax' });
|
|
105
|
+
// Set a cross-site cookie for third-party contexts
|
|
106
|
+
// res.cookie('cookie2', 'value2', { sameSite: 'none', secure: true });
|
|
107
|
+
|
|
108
|
+
res.end( response_output );
|
|
109
|
+
break;
|
|
110
|
+
case 'POST':
|
|
111
|
+
case 'PUT':
|
|
112
|
+
let data = '';
|
|
113
|
+
req.on('data', chunk => {
|
|
114
|
+
data += chunk;
|
|
115
|
+
})
|
|
116
|
+
req.on('end', async () => {
|
|
117
|
+
res.statusCode = status_code;
|
|
118
|
+
res.setHeader( 'Content-Type', content_types[content_type] );
|
|
90
119
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
}
|
|
120
|
+
// Cookies
|
|
121
|
+
res.setHeader('Set-Cookie','uuid=1234-1324-1234-1234-1234; Max-Age=3000; SameSite=lax; Secure');
|
|
95
122
|
|
|
96
|
-
|
|
97
|
-
res.setHeader( 'Content-Type', content_types[content_type] );
|
|
123
|
+
response_output = await controller.view( data );
|
|
98
124
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
// Set a cross-site cookie for third-party contexts
|
|
104
|
-
// res.cookie('cookie2', 'value2', { sameSite: 'none', secure: true });
|
|
105
|
-
|
|
106
|
-
res.end( response_output );
|
|
125
|
+
res.end( response_output );
|
|
126
|
+
})
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
107
129
|
} else if ( request_url.pathname.split('/')[1] == 'assets' ) {
|
|
108
130
|
/**
|
|
109
131
|
* Serve static files from public directory
|
package/index.js
CHANGED
|
@@ -61,15 +61,30 @@ FW = function ( config ) {
|
|
|
61
61
|
* @param {object} res The response object
|
|
62
62
|
*/
|
|
63
63
|
Controller = function Controller ( route, req, res ) {
|
|
64
|
-
// console.log("Controller in index:\n",
|
|
64
|
+
// console.log("Controller in index (req.method):\n", req.method);
|
|
65
|
+
// console.log("Controller in index (req):\n", req);
|
|
65
66
|
this.route = route;
|
|
66
67
|
this.req = req;
|
|
67
68
|
this.res = res;
|
|
69
|
+
this.route.method = req.method;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Return view
|
|
73
|
+
*/
|
|
74
|
+
this.view = async function ( data = null ) {
|
|
75
|
+
route.data = data;
|
|
76
|
+
route.req = this.req;
|
|
77
|
+
route.res = this.res;
|
|
78
|
+
let output = await route.action( route );
|
|
79
|
+
|
|
80
|
+
return output;
|
|
81
|
+
}
|
|
68
82
|
|
|
69
83
|
/**
|
|
70
84
|
* Check for authorization parameters.
|
|
71
85
|
*/
|
|
72
86
|
if ( route.params !== null ) {
|
|
87
|
+
// console.log("Params not null", route.params);
|
|
73
88
|
if ( route.params.access !== undefined ) {
|
|
74
89
|
console.log("This route should be authorized!");
|
|
75
90
|
}
|
package/models.js
CHANGED
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
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const url = require('url');
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* This is the router for the fw framework.
|
|
3
5
|
*
|
|
@@ -30,7 +32,7 @@ global.ROUTE = function ( route, action, params = null ) {
|
|
|
30
32
|
|
|
31
33
|
function Router ( req, url ) {
|
|
32
34
|
const method = req.method;
|
|
33
|
-
console.log("Router url:", url);
|
|
35
|
+
// console.log("Router url:", url);
|
|
34
36
|
// Split url into array
|
|
35
37
|
let url_array = url.pathname.split('/');
|
|
36
38
|
|
|
@@ -56,18 +58,22 @@ function Router ( req, url ) {
|
|
|
56
58
|
};
|
|
57
59
|
|
|
58
60
|
function findRoute ( url ) {
|
|
61
|
+
let pathname = url.pathname;
|
|
62
|
+
// console.log( "ROUTE, url", url );
|
|
63
|
+
|
|
64
|
+
// console.log("THIS", this);
|
|
59
65
|
// Check if route exist
|
|
60
|
-
let route = global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes(
|
|
66
|
+
let route = global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes( pathname ) );
|
|
61
67
|
|
|
62
68
|
if ( route ) {
|
|
63
69
|
// console.log("Route defined", url);
|
|
64
70
|
// Execute action for route
|
|
65
71
|
return route;
|
|
66
72
|
} else {
|
|
67
|
-
const url_array =
|
|
73
|
+
const url_array = pathname.split('/').filter( e => e );
|
|
68
74
|
|
|
69
75
|
// Check if route is an API call
|
|
70
|
-
let isAPICall =
|
|
76
|
+
let isAPICall = pathname.split('/')[1] === 'api' ? true : false;
|
|
71
77
|
|
|
72
78
|
if ( isAPICall ) {
|
|
73
79
|
// console.log("Route NOT defined (API)", url, url.split('/')[1]);
|
|
@@ -98,15 +104,17 @@ function findRoute ( url ) {
|
|
|
98
104
|
break;
|
|
99
105
|
}
|
|
100
106
|
|
|
101
|
-
let regex_result =
|
|
102
|
-
let regex_groups = regex_result.groups;
|
|
107
|
+
let regex_result = pathname.match(regex);
|
|
103
108
|
|
|
104
|
-
|
|
105
|
-
|
|
109
|
+
if ( regex_result ) {
|
|
110
|
+
let regex_groups = regex_result.groups;
|
|
111
|
+
route.resource = regex_groups.resource;
|
|
112
|
+
route.resource_id = regex_groups.id;
|
|
113
|
+
}
|
|
106
114
|
|
|
107
115
|
return route;
|
|
108
116
|
} else {
|
|
109
|
-
return global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes(
|
|
117
|
+
return global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes( pathname ) );
|
|
110
118
|
}
|
|
111
119
|
}
|
|
112
120
|
}
|