@blaasvaer/frmwrk 0.1.13 → 0.1.16
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 +82 -69
- package/index.js +16 -1
- package/models.js +0 -1
- package/package 2.json +30 -0
- package/package.json +2 -2
- package/router.js +20 -10
package/handle-request.js
CHANGED
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const path = require('path');
|
|
5
|
-
const statics = require('
|
|
6
|
-
const
|
|
7
|
-
const { findRoute, Router } = require('./router');
|
|
5
|
+
const statics = require('serve-handler');
|
|
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,71 +61,85 @@ 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
|
+
if ( ! controller.route.params ) {
|
|
68
|
+
/**
|
|
69
|
+
* If the route has no parameters, then just run the route method and serve whatever it may …
|
|
70
|
+
*/
|
|
71
|
+
response_output = await controller.view();
|
|
72
|
+
} else {
|
|
73
|
+
/**
|
|
74
|
+
* Authorization
|
|
75
|
+
*/
|
|
76
|
+
if ( Authorize( controller.route ) ) {
|
|
77
|
+
response_output = await controller.view();
|
|
78
|
+
} else {
|
|
79
|
+
/**
|
|
80
|
+
* Redirect the user to login if auth fails
|
|
81
|
+
*/
|
|
82
|
+
if ( controller.route.params.access.redirect ) {
|
|
83
|
+
status_code = 302;
|
|
84
|
+
res.setHeader( 'Location', controller.route.params.access.redirect );
|
|
85
|
+
}
|
|
86
|
+
// console.log("NO USER", controller.route.params.redirect);
|
|
87
|
+
// response_output = redirect.action.call();
|
|
88
|
+
// route.params.redirect.call();
|
|
89
|
+
// response_output = 'Access denied';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if ( controller.route.params.type ) {
|
|
93
|
+
content_type = controller.route.params.type;
|
|
94
|
+
}
|
|
84
95
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
//
|
|
89
|
-
|
|
96
|
+
|
|
97
|
+
res.statusCode = status_code;
|
|
98
|
+
res.setHeader( 'Content-Type', content_types[content_type] );
|
|
99
|
+
// res.setHeader( 'Content-Security-Policy', "default-src 'self'; font-src 'self'; img-src 'self'; script-src 'self'; style-src 'self'; frame-src 'self'");
|
|
100
|
+
// res.setHeader( 'Content-Security-Policy', "default-src 'self'; img-src 'self';");
|
|
101
|
+
|
|
102
|
+
// Cookies
|
|
103
|
+
res.setHeader('Set-Cookie','uuid=1234-1324-1234-1234-1234; Max-Age=3000; SameSite=lax; Secure');
|
|
104
|
+
// Set a same-site cookie for first-party contexts
|
|
105
|
+
// res.cookie('cookie1', 'value1', { sameSite: 'lax' });
|
|
106
|
+
// Set a cross-site cookie for third-party contexts
|
|
107
|
+
// res.cookie('cookie2', 'value2', { sameSite: 'none', secure: true });
|
|
108
|
+
|
|
109
|
+
res.end( response_output );
|
|
110
|
+
break;
|
|
111
|
+
case 'POST':
|
|
112
|
+
case 'PUT':
|
|
113
|
+
case 'PATCH':
|
|
114
|
+
case 'UPDATE':
|
|
115
|
+
let data = '';
|
|
116
|
+
req.on('data', chunk => {
|
|
117
|
+
data += chunk;
|
|
118
|
+
})
|
|
119
|
+
req.on('end', async () => {
|
|
120
|
+
res.statusCode = status_code;
|
|
121
|
+
res.setHeader( 'Content-Type', content_types[content_type] );
|
|
90
122
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
123
|
+
// Cookies
|
|
124
|
+
res.setHeader( 'Set-Cookie','uuid=1234-1324-1234-1234-1234; Max-Age=3000; SameSite=lax; Secure' );
|
|
125
|
+
res.setHeader( 'Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE, OPTIONS' );
|
|
126
|
+
// res.setHeader( 'Content-Security-Policy', "default-src 'self'; img-src 'self';");
|
|
95
127
|
|
|
96
|
-
|
|
97
|
-
res.setHeader( 'Content-Type', content_types[content_type] );
|
|
128
|
+
response_output = await controller.view( data );
|
|
98
129
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
// res.cookie('cookie2', 'value2', { sameSite: 'none', secure: true });
|
|
105
|
-
|
|
106
|
-
res.end( response_output );
|
|
107
|
-
} else if ( request_url.pathname.split('/')[1] == 'themes' ) {
|
|
130
|
+
res.end( response_output );
|
|
131
|
+
})
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
} else if ( request_url.pathname.split('/')[1] == 'assets' ) {
|
|
108
135
|
/**
|
|
109
136
|
* Serve static files from public directory
|
|
110
137
|
*/
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
let file_server = new statics.Server();
|
|
138
|
+
statics( req, res, {
|
|
139
|
+
public: 'public'
|
|
140
|
+
} );
|
|
116
141
|
|
|
117
|
-
|
|
118
|
-
file_server.serve(req, res, function ( err, result ) {
|
|
119
|
-
if ( err ) { console.error("Error serving " + url + " - " + err.message);
|
|
120
|
-
res.writeHead( err.status, err.headers );
|
|
121
|
-
res.end();
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
}).resume();
|
|
125
|
-
|
|
126
|
-
// } else if ( SOME_CONTROLLER_EXISTS ) {
|
|
127
|
-
/**
|
|
128
|
-
* Check for controller to be able to access a default action and pass in any data or parameters
|
|
129
|
-
*/
|
|
142
|
+
return;
|
|
130
143
|
} else {
|
|
131
144
|
res.statusCode = 404;
|
|
132
145
|
res.setHeader('Content-Type', 'text/plain; charset=utf-8' );
|
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blaasvaer/frmwrk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.16",
|
|
4
4
|
"description": "My personal Node framework",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -25,6 +25,6 @@
|
|
|
25
25
|
},
|
|
26
26
|
"homepage": "https://bitbucket.org/blaasvaer/frmwrk#readme",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"
|
|
28
|
+
"serve-handler": "^6.1.3"
|
|
29
29
|
}
|
|
30
30
|
}
|
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,24 @@ 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
|
-
// console.log("Route defined", url);
|
|
69
|
+
// console.log("Route defined url:", url);
|
|
70
|
+
// console.log("Route defined route:", route);
|
|
64
71
|
// Execute action for route
|
|
65
72
|
return route;
|
|
66
73
|
} else {
|
|
67
|
-
|
|
74
|
+
// console.log("Route NOT defined", url);
|
|
75
|
+
const url_array = pathname.split('/').filter( e => e );
|
|
68
76
|
|
|
69
77
|
// Check if route is an API call
|
|
70
|
-
let isAPICall =
|
|
78
|
+
let isAPICall = pathname.split('/')[1] === 'api' ? true : false;
|
|
71
79
|
|
|
72
80
|
if ( isAPICall ) {
|
|
73
81
|
// console.log("Route NOT defined (API)", url, url.split('/')[1]);
|
|
@@ -98,15 +106,17 @@ function findRoute ( url ) {
|
|
|
98
106
|
break;
|
|
99
107
|
}
|
|
100
108
|
|
|
101
|
-
let regex_result =
|
|
102
|
-
let regex_groups = regex_result.groups;
|
|
109
|
+
let regex_result = pathname.match(regex);
|
|
103
110
|
|
|
104
|
-
|
|
105
|
-
|
|
111
|
+
if ( regex_result ) {
|
|
112
|
+
let regex_groups = regex_result.groups;
|
|
113
|
+
route.resource = regex_groups.resource;
|
|
114
|
+
route.resource_id = regex_groups.id;
|
|
115
|
+
}
|
|
106
116
|
|
|
107
117
|
return route;
|
|
108
118
|
} else {
|
|
109
|
-
return global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes(
|
|
119
|
+
return global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes( pathname ) );
|
|
110
120
|
}
|
|
111
121
|
}
|
|
112
122
|
}
|