satto 1.1.1 → 1.1.2

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 +39 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "satto",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
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
@@ -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 };
@@ -102,12 +128,21 @@ function createServer(__dirname, routes = [], port = 3000) {
102
128
  res.send(await renderPage(html, params));
103
129
  } catch (err) {
104
130
  console.error(err);
105
- res.status(500).send();
131
+ next(err);
106
132
  }
107
133
  });
108
134
  });
109
135
  });
110
136
 
137
+ app.use((req, res) => {
138
+ res.status(404).send(renderErrorPage(404, "Page Not Found"));
139
+ });
140
+
141
+ app.use((err, req, res, next) => {
142
+ console.error(err);
143
+ res.status(500).send(renderErrorPage(500, "Internal Server Error"));
144
+ });
145
+
111
146
  app.listen(port, () => {
112
147
  const banner = [
113
148
  " ____ _ _ ",