satto 1.1.0 → 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 +38 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "satto",
3
- "version": "1.1.0",
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();
@@ -64,20 +86,22 @@ function createServer(__dirname, routes = [], port = 3000) {
64
86
  app.set("views", path.join(__dirname, "app"));
65
87
  app.engine("html", ejs.renderFile);
66
88
  app.set("view engine", "html");
67
-
68
- app.use(express.static(path.join(__dirname, "app")));
69
89
  app.use(express.static(path.join(__dirname, "static")));
70
90
 
71
91
  routes.forEach((route) => {
92
+ app.use(express.static(path.join(__dirname, "app", route.page)));
72
93
  app.get(route.path, (req, res, next) => {
94
+ if(req.path.includes('.')){
95
+ return next();
96
+ }
97
+
73
98
  const page = route.page;
74
99
  const htmlPath = path.join(__dirname, "app", page, page + ".html");
75
- app.use(express.static(path.join(__dirname, "app", page)));
76
100
 
77
101
  fs.readFile(htmlPath, "utf8", async (err, html) => {
78
102
  try {
79
103
  if (err) {
80
- res.status(404).send();
104
+ return next();
81
105
  }
82
106
 
83
107
  const params = { ...req.params };
@@ -104,12 +128,21 @@ function createServer(__dirname, routes = [], port = 3000) {
104
128
  res.send(await renderPage(html, params));
105
129
  } catch (err) {
106
130
  console.error(err);
107
- res.status(500).send();
131
+ next(err);
108
132
  }
109
133
  });
110
134
  });
111
135
  });
112
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
+
113
146
  app.listen(port, () => {
114
147
  const banner = [
115
148
  " ____ _ _ ",