slower 1.1.13 → 1.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/lib/router.js +7 -5
- package/lib/utils.js +3 -3
- package/package.json +1 -1
- package/readme.md +11 -1
package/lib/router.js
CHANGED
|
@@ -113,7 +113,8 @@ class SlowerRouter {
|
|
|
113
113
|
* }));
|
|
114
114
|
*/
|
|
115
115
|
setDynamic = function (path, file = '', mime = '', replacementData = null) {
|
|
116
|
-
let encoding = (mime === SlowerRouter.mime_table['default'] ? undefined : 'utf-8')
|
|
116
|
+
let encoding = (mime === SlowerRouter.mime_table['default'] ? undefined : 'utf-8');
|
|
117
|
+
if (file.includes('{%}')) { file = file.replace(/\{\%\}/gim, path.replace(/\//gim,'')); }
|
|
117
118
|
let stat = new Route(path, 'GET', (req, res) => {
|
|
118
119
|
let data, targetFile, extension, targetMime, targetEncoding;
|
|
119
120
|
if (fs.existsSync(file) && fs.lstatSync(file).isDirectory()) {
|
|
@@ -153,10 +154,11 @@ class SlowerRouter {
|
|
|
153
154
|
* @param {String} mime The file's mime type
|
|
154
155
|
* @returns {Object} An Route object already configured
|
|
155
156
|
* @example <caption> Defining a simple GET route:</caption>
|
|
156
|
-
* setStatic('/login', __dirname+'/public/static/views/
|
|
157
|
+
* setStatic('/login', __dirname+'/public/static/views/{%}.html', 'text/html');
|
|
157
158
|
*/
|
|
158
159
|
setStatic = function (path, file = '', mime = '') {
|
|
159
|
-
let encoding = (mime === SlowerRouter.mime_table['default'] ? undefined : 'utf-8')
|
|
160
|
+
let encoding = (mime === SlowerRouter.mime_table['default'] ? undefined : 'utf-8');
|
|
161
|
+
if (file.includes('{%}')) { file = file.replace(/\{\%\}/gim, path.replace(/\//gim,'')); }
|
|
160
162
|
let stat = new Route(path, 'GET', (req, res) => {
|
|
161
163
|
let data, targetFile, extension, targetMime, targetEncoding;
|
|
162
164
|
if (fs.existsSync(file) && fs.lstatSync(file).isDirectory()) {
|
|
@@ -223,7 +225,7 @@ class SlowerRouter {
|
|
|
223
225
|
*/
|
|
224
226
|
setFallbackFile = function (file = '', mime = '', replacementData = null) {
|
|
225
227
|
this.fallback = function fb (req,res) {
|
|
226
|
-
let encoding = (mime === SlowerRouter.mime_table['default'] ? undefined : 'utf-8')
|
|
228
|
+
let encoding = (mime === SlowerRouter.mime_table['default'] ? undefined : 'utf-8');
|
|
227
229
|
let data, targetFile, extension, targetMime, targetEncoding;
|
|
228
230
|
if (fs.existsSync(file) && fs.lstatSync(file).isDirectory()) {
|
|
229
231
|
targetFile = file.replace(/\//gim, '\\');
|
|
@@ -330,7 +332,7 @@ class SlowerRouter {
|
|
|
330
332
|
});
|
|
331
333
|
callback(server);
|
|
332
334
|
if (!host) host = undefined; // Turn falsy values into undefined, for default behaviour
|
|
333
|
-
|
|
335
|
+
server.listen(port, host);
|
|
334
336
|
return this;
|
|
335
337
|
}
|
|
336
338
|
}
|
package/lib/utils.js
CHANGED
|
@@ -44,7 +44,7 @@ const isSparseEqual = (str1 = '', str2 = '') => {
|
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
46
|
* It's a template engine, to render HTML containing template spaces.
|
|
47
|
-
* The charset for replacement is
|
|
47
|
+
* The charset for replacement is {{content}}
|
|
48
48
|
* @since 1.2.5
|
|
49
49
|
*
|
|
50
50
|
* @param {String} html The HTML code
|
|
@@ -52,7 +52,7 @@ const isSparseEqual = (str1 = '', str2 = '') => {
|
|
|
52
52
|
* @return {String} The HTML with the templates replaces
|
|
53
53
|
*
|
|
54
54
|
* @example <caption> Rendering: </caption>
|
|
55
|
-
* var template = 'Hello, my name is
|
|
55
|
+
* var template = 'Hello, my name is {{name}}. I\\'m {{age}} years old.';
|
|
56
56
|
* console.log(TemplateEngine(template, {
|
|
57
57
|
* name: "Krasimir",
|
|
58
58
|
* age: 29
|
|
@@ -62,7 +62,7 @@ const renderDynamicHTML = (html, patterns) => {
|
|
|
62
62
|
let template = html;
|
|
63
63
|
for (let item in patterns) {
|
|
64
64
|
template = html.replace(
|
|
65
|
-
new RegExp('
|
|
65
|
+
new RegExp('{{'+item+'}}', 'gim'),
|
|
66
66
|
patterns[item]
|
|
67
67
|
);
|
|
68
68
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -53,6 +53,11 @@ app.setDynamic(string: path, string: file = '', string: mime = '', object: repla
|
|
|
53
53
|
> This is a custom file-response route, configured for template rendering just before response.
|
|
54
54
|
> Providing an object as 'replacementData' in this format { valueToBeReplaced: valueToReplace },
|
|
55
55
|
allows for template rendering. The value to replace in the file, uses this notation: '<{content}>'.
|
|
56
|
+
> URL reference in filename:
|
|
57
|
+
> For direct references, it is possible to use the token '{%}' to replace the filename for the URL.
|
|
58
|
+
> Ex:
|
|
59
|
+
app.setStatic('/login', './templates/{%}.html', 'text/html');
|
|
60
|
+
This will access the 'login.html' file when the route '/login' is accessed.
|
|
56
61
|
> Example:
|
|
57
62
|
Responding a route for '/custom' with file 'custom.html':
|
|
58
63
|
app.setDynamic('/custom', './templates/custom.html', 'text/html', { smile: ':)' })
|
|
@@ -66,6 +71,11 @@ app.setDynamic(string: path, string: file = '', string: mime = '', object: repla
|
|
|
66
71
|
app.setStatic(string: path, string: file = '', string: mime = ''): this
|
|
67
72
|
|
|
68
73
|
> Creates a new GET route for path defined in 'path', responding with the specified file and MIME type.
|
|
74
|
+
> URL reference in filename:
|
|
75
|
+
> For direct references, it is possible to use the token '{%}' to replace the filename for the URL.
|
|
76
|
+
> Ex:
|
|
77
|
+
app.setStatic('/login', './templates/{%}.html', 'text/html');
|
|
78
|
+
This will access the 'login.html' file when the route '/login' is accessed.
|
|
69
79
|
> Example: A route for '/login' page, responding with 'login.html' file
|
|
70
80
|
setStatic('/login', __dirname+'/public/static/views/login.html', 'text/html');
|
|
71
81
|
> Returns the own object instance, so that methods can be chained.
|
|
@@ -116,7 +126,7 @@ app.setMiddleware((req, res) => {
|
|
|
116
126
|
console.log(`${req.time} - ${req.method} : ${req.url}`);
|
|
117
127
|
});
|
|
118
128
|
|
|
119
|
-
app.setStatic('/favicon.ico', __dirname+'/public/
|
|
129
|
+
app.setStatic('/favicon.ico', __dirname+'/public/{%}');
|
|
120
130
|
|
|
121
131
|
app.setRoute('/', 'GET', (req, res) => {
|
|
122
132
|
res.writeHead(200, { 'Content-Type': 'text/html' });
|