slower 1.1.13 → 1.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/lib/router.js CHANGED
@@ -29,6 +29,7 @@ class SlowerRouter {
29
29
  'js' : ['text/javascript', 'utf-8'],
30
30
  'css' : ['text/css', 'utf-8'],
31
31
  'ico' : ['image/png'],
32
+ 'json': ['application/json'],
32
33
  'default': ['application/octet-stream']
33
34
  }
34
35
 
@@ -113,7 +114,8 @@ class SlowerRouter {
113
114
  * }));
114
115
  */
115
116
  setDynamic = function (path, file = '', mime = '', replacementData = null) {
116
- let encoding = (mime === SlowerRouter.mime_table['default'] ? undefined : 'utf-8')
117
+ let encoding = (mime === SlowerRouter.mime_table['default'] ? undefined : 'utf-8');
118
+ if (file.includes('{%}')) { file = file.replace(/\{\%\}/gim, path.replace(/\//gim,'')); }
117
119
  let stat = new Route(path, 'GET', (req, res) => {
118
120
  let data, targetFile, extension, targetMime, targetEncoding;
119
121
  if (fs.existsSync(file) && fs.lstatSync(file).isDirectory()) {
@@ -153,10 +155,11 @@ class SlowerRouter {
153
155
  * @param {String} mime The file's mime type
154
156
  * @returns {Object} An Route object already configured
155
157
  * @example <caption> Defining a simple GET route:</caption>
156
- * setStatic('/login', __dirname+'/public/static/views/login.html', 'text/html');
158
+ * setStatic('/login', __dirname+'/public/static/views/{%}.html', 'text/html');
157
159
  */
158
160
  setStatic = function (path, file = '', mime = '') {
159
- let encoding = (mime === SlowerRouter.mime_table['default'] ? undefined : 'utf-8')
161
+ let encoding = (mime === SlowerRouter.mime_table['default'] ? undefined : 'utf-8');
162
+ if (file.includes('{%}')) { file = file.replace(/\{\%\}/gim, path.replace(/\//gim,'')); }
160
163
  let stat = new Route(path, 'GET', (req, res) => {
161
164
  let data, targetFile, extension, targetMime, targetEncoding;
162
165
  if (fs.existsSync(file) && fs.lstatSync(file).isDirectory()) {
@@ -223,7 +226,7 @@ class SlowerRouter {
223
226
  */
224
227
  setFallbackFile = function (file = '', mime = '', replacementData = null) {
225
228
  this.fallback = function fb (req,res) {
226
- let encoding = (mime === SlowerRouter.mime_table['default'] ? undefined : 'utf-8')
229
+ let encoding = (mime === SlowerRouter.mime_table['default'] ? undefined : 'utf-8');
227
230
  let data, targetFile, extension, targetMime, targetEncoding;
228
231
  if (fs.existsSync(file) && fs.lstatSync(file).isDirectory()) {
229
232
  targetFile = file.replace(/\//gim, '\\');
@@ -330,7 +333,7 @@ class SlowerRouter {
330
333
  });
331
334
  callback(server);
332
335
  if (!host) host = undefined; // Turn falsy values into undefined, for default behaviour
333
- // return server.listen(port, host);
336
+ server.listen(port, host);
334
337
  return this;
335
338
  }
336
339
  }
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 <{content}>
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 <{name}>. I\\'m <{age}> years old.';
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('<{'+item+'}>', 'gim'),
65
+ new RegExp('{{'+item+'}}', 'gim'),
66
66
  patterns[item]
67
67
  );
68
68
  }
@@ -76,7 +76,7 @@ const setSocketLocals = (socket) => {
76
76
  socket.session.host = (
77
77
  socket.socket.localAddress.startsWith('::') ?
78
78
  socket.socket.localAddress.substring(
79
- socket.socketlocalAddress.indexOf(':',2)+1
79
+ socket.socket.localAddress.indexOf(':',2)+1
80
80
  ) : socket.socket.localAddress);
81
81
  socket.session.rhost = (
82
82
  socket.socket.remoteAddress.startsWith('::') ?
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slower",
3
- "version": "1.1.13",
3
+ "version": "1.1.16",
4
4
  "description": "A package for simple HTTP server routing.",
5
5
  "main": "index.js",
6
6
  "directories": {
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/favicon.ico');
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' });