satto 1.1.1 → 1.1.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "satto",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "A minimal server-rendered web framework for Node.js",
5
5
  "main": "src/lib/index.js",
6
6
  "bin": {
@@ -29,6 +29,6 @@
29
29
  "express": "^5.2.1"
30
30
  },
31
31
  "overrides": {
32
- "minimatch": "10.2.1"
32
+ "minimatch": "10.2.4"
33
33
  }
34
34
  }
package/src/bin/satto.js CHANGED
@@ -69,7 +69,7 @@ createServer(__dirname, routes);
69
69
  "build": "satto run build"
70
70
  },
71
71
  "overrides": {
72
- "minimatch": "10.2.1"
72
+ "minimatch": "10.2.4"
73
73
  }
74
74
  }`
75
75
  };
package/src/lib/index.js CHANGED
@@ -57,6 +57,28 @@ async function renderPage(html, params) {
57
57
  }
58
58
  }
59
59
 
60
+ function renderErrorPage(code, message) {
61
+ return `
62
+ <style>
63
+ body {
64
+ margin: 0;
65
+ height: 100vh;
66
+ display: flex;
67
+ justify-content: center;
68
+ align-items: center;
69
+ background: black;
70
+ color: white;
71
+ font-family: system-ui, sans-serif;
72
+ }
73
+ h1 {
74
+ font-size: 2rem;
75
+ font-weight: 400;
76
+ }
77
+ </style>
78
+ <h1>${code} — ${message}</h1>
79
+ `;
80
+ }
81
+
60
82
  function createServer(__dirname, routes = [], port = 3000) {
61
83
  const versionApp = Date.now();
62
84
  const app = express();
@@ -67,15 +89,19 @@ function createServer(__dirname, routes = [], port = 3000) {
67
89
  app.use(express.static(path.join(__dirname, "static")));
68
90
 
69
91
  routes.forEach((route) => {
70
- app.get(route.path, (req, res) => {
92
+ app.use(express.static(path.join(__dirname, "app", route.page)));
93
+ app.get(route.path, (req, res, next) => {
94
+ if(req.path.includes('.')){
95
+ return next();
96
+ }
97
+
71
98
  const page = route.page;
72
99
  const htmlPath = path.join(__dirname, "app", page, page + ".html");
73
- app.use(express.static(path.join(__dirname, "app", page)));
74
100
 
75
101
  fs.readFile(htmlPath, "utf8", async (err, html) => {
76
102
  try {
77
103
  if (err) {
78
- res.status(404).send();
104
+ return next();
79
105
  }
80
106
 
81
107
  const params = { ...req.params };
@@ -101,13 +127,28 @@ function createServer(__dirname, routes = [], port = 3000) {
101
127
 
102
128
  res.send(await renderPage(html, params));
103
129
  } catch (err) {
104
- console.error(err);
105
- res.status(500).send();
130
+ next(err);
106
131
  }
107
132
  });
108
133
  });
109
134
  });
110
135
 
136
+ app.use((req, res) => {
137
+ res.status(404).send(renderErrorPage(404, "Page Not Found"));
138
+ });
139
+
140
+ app.use((err, req, res, next) => {
141
+ console.error(err);
142
+
143
+ const isFetchError = err.message?.includes('fetch failed') || err.code === 'ECONNREFUSED' || err.code === 'ENOTFOUND';
144
+
145
+ if (isFetchError) {
146
+ return res.status(404).send(renderErrorPage(404, "Page Not Found"));
147
+ }
148
+
149
+ res.status(500).send(renderErrorPage(500, "Internal Server Error"));
150
+ });
151
+
111
152
  app.listen(port, () => {
112
153
  const banner = [
113
154
  " ____ _ _ ",