satto 1.0.8 → 1.1.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/lib/index.js +48 -44
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "satto",
3
- "version": "1.0.8",
3
+ "version": "1.1.0",
4
4
  "description": "A minimal server-rendered web framework for Node.js",
5
5
  "main": "src/lib/index.js",
6
6
  "bin": {
package/src/lib/index.js CHANGED
@@ -18,38 +18,42 @@ function applyParams(template, params) {
18
18
  }
19
19
 
20
20
  async function renderPage(html, params) {
21
- const match = html.match(/<ssr\s+url="([^"]+)"\s+.*response="([^"]+)"[^>]*>/);
21
+ try {
22
+ const match = html.match(/<ssr\s+url="([^"]+)"\s+.*response="([^"]+)"[^>]*>/);
22
23
 
23
- if(match) {
24
- const url = applyParams(match?.[1], params);
25
- const resVar = match?.[2];
26
- let data = {};
24
+ if (match) {
25
+ const url = applyParams(match?.[1], params);
26
+ const resVar = match?.[2];
27
+ let data = {};
27
28
 
28
- if (url) {
29
- const res = await fetch(url);
30
- data = await res.json();
31
- }
29
+ if (url) {
30
+ const res = await fetch(url);
31
+ data = await res.json();
32
+ }
32
33
 
33
- html = html.replace(/<ssr[^>]*>([\s\S]*?)<\/ssr>/g, (_, content) => {
34
- return content.replace(
35
- /<ssr[^>]*>|<\/ssr>|\{\{(.*?)\}\}|<for\s+condition="let\s+(.+?)\s+in\s+(.+?)"\s*>|<\/for>|<if\s+condition="(.*?)"\s*>|<\/if>|\[(.+?)\]="(.+?)"/g,
36
- (match, expr, item, arr, cond, attr, val) => {
37
- if (match.startsWith("<ssr")) return "";
38
- if (match === "</ssr>") return "";
39
- if (expr) return `<%= ${expr.trim()} %>`;
40
- if (item && arr) return `<% ${arr}.forEach(${item} => { %>`;
41
- if (match === "</for>") return "<% }) %>";
42
- if (cond) return `<% if (${cond}) { %>`;
43
- if (match === "</if>") return "<% } %>";
44
- if (attr && val) return `${attr}="<%= ${val} %>"`;
45
- return match;
46
- }
47
- );
48
- });
34
+ html = html.replace(/<ssr[^>]*>([\s\S]*?)<\/ssr>/g, (_, content) => {
35
+ return content.replace(
36
+ /<ssr[^>]*>|<\/ssr>|\{\{(.*?)\}\}|<for\s+condition="let\s+(.+?)\s+in\s+(.+?)"\s*>|<\/for>|<if\s+condition="(.*?)"\s*>|<\/if>|\[(.+?)\]="(.+?)"/g,
37
+ (match, expr, item, arr, cond, attr, val) => {
38
+ if (match.startsWith("<ssr")) return "";
39
+ if (match === "</ssr>") return "";
40
+ if (expr) return `<%= ${expr.trim()} %>`;
41
+ if (item && arr) return `<% ${arr}.forEach(${item} => { %>`;
42
+ if (match === "</for>") return "<% }) %>";
43
+ if (cond) return `<% if (${cond}) { %>`;
44
+ if (match === "</if>") return "<% } %>";
45
+ if (attr && val) return `${attr}="<%= ${val} %>"`;
46
+ return match;
47
+ }
48
+ );
49
+ });
49
50
 
50
- return ejs.render(html, {params, [resVar]: data});
51
- } else {
52
- return html;
51
+ return ejs.render(html, { params, [resVar]: data });
52
+ } else {
53
+ return html;
54
+ }
55
+ } catch (err) {
56
+ throw err;
53
57
  }
54
58
  }
55
59
 
@@ -65,15 +69,15 @@ function createServer(__dirname, routes = [], port = 3000) {
65
69
  app.use(express.static(path.join(__dirname, "static")));
66
70
 
67
71
  routes.forEach((route) => {
68
- app.get(route.path, async (req, res) => {
69
- try {
70
- const page = route.page;
71
- const htmlPath = path.join(__dirname, "app", page, page + ".html");
72
- app.use(express.static(path.join(__dirname, "app", page)));
72
+ app.get(route.path, (req, res, next) => {
73
+ const page = route.page;
74
+ const htmlPath = path.join(__dirname, "app", page, page + ".html");
75
+ app.use(express.static(path.join(__dirname, "app", page)));
73
76
 
74
- fs.readFile(htmlPath, "utf8", async (err, html) => {
77
+ fs.readFile(htmlPath, "utf8", async (err, html) => {
78
+ try {
75
79
  if (err) {
76
- return res.status(404).send();
80
+ res.status(404).send();
77
81
  }
78
82
 
79
83
  const params = { ...req.params };
@@ -82,27 +86,27 @@ function createServer(__dirname, routes = [], port = 3000) {
82
86
  const CssPath = path.join(__dirname, "app", page, page + ".css");
83
87
  const JsPath = path.join(__dirname, "app", page, page + ".js");
84
88
 
85
- if(hasContent(JsPath)) {
89
+ if (hasContent(JsPath)) {
86
90
  html += `\n<script src="./${page}.js?v=${versionApp}"></script>`;
87
91
  }
88
92
 
89
93
  html = index.replace("<routes></routes>", html);
90
94
  html = html.replace(`<link rel="stylesheet" href="/styles.css">`, `<link rel="stylesheet" href="/styles.css?v=${versionApp}">`);
91
95
 
92
- if(hasContent(CssPath)) {
96
+ if (hasContent(CssPath)) {
93
97
  html = html.replace(
94
- "</head>",
98
+ "</head>",
95
99
  `<link rel="stylesheet" href="./${page}.css?v=${versionApp}">
96
- </head>`
100
+ </head>`
97
101
  );
98
102
  }
99
103
 
100
104
  res.send(await renderPage(html, params));
101
- });
102
- } catch (err) {
103
- console.error(err);
104
- res.status(500).send();
105
- }
105
+ } catch (err) {
106
+ console.error(err);
107
+ res.status(500).send();
108
+ }
109
+ });
106
110
  });
107
111
  });
108
112