@kaliber/build 0.0.152-beta.1 → 0.0.152-beta.2
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/serve.js +20 -11
- package/package.json +1 -1
package/lib/serve.js
CHANGED
@@ -69,12 +69,7 @@ app.use((err, req, res, next) => {
|
|
69
69
|
console.error(err)
|
70
70
|
if (reportError) reportError(err, req)
|
71
71
|
|
72
|
-
|
73
|
-
if (isProduction) {
|
74
|
-
findFile(req.path, internalServerError)
|
75
|
-
.then(file => file ? response.sendFile(file) : next())
|
76
|
-
.catch(next)
|
77
|
-
} else response.send(`<pre><title style='display: block;'>${err.stack || err.toString()}</title><pre>`)
|
72
|
+
serveInternalServerError(err, { req, res, next })
|
78
73
|
})
|
79
74
|
|
80
75
|
app.listen(port, () => console.log(`Server listening at port ${port}`))
|
@@ -84,7 +79,7 @@ async function resolveFile(req, res, next) {
|
|
84
79
|
const { path } = req
|
85
80
|
/** @type {Array<[string, (file:any) => any]>} */
|
86
81
|
const combinations = [
|
87
|
-
[indexWithRouting, file => serveIndexWithRouting(req, res,
|
82
|
+
[indexWithRouting, file => serveIndexWithRouting(file, { req, res, next })],
|
88
83
|
[notFound, file => res.status(404).sendFile(file)],
|
89
84
|
[index, file => res.status(200).sendFile(file)],
|
90
85
|
]
|
@@ -138,7 +133,7 @@ function possibleDirectories(path) {
|
|
138
133
|
return possibleDirectories
|
139
134
|
}
|
140
135
|
|
141
|
-
function serveIndexWithRouting(req, res,
|
136
|
+
function serveIndexWithRouting(file, { req, res, next }) {
|
142
137
|
const envRequire = isProduction ? require : require('import-fresh')
|
143
138
|
|
144
139
|
const routeTemplate = envRequire(file)
|
@@ -153,10 +148,15 @@ function serveIndexWithRouting(req, res, file) {
|
|
153
148
|
const html = renderTemplate(template, location, { data })
|
154
149
|
res.status(status).set(headers).send(html)
|
155
150
|
})
|
151
|
+
.catch(error => serveInternalServerError(error, { req, res, next }))
|
156
152
|
else {
|
157
|
-
|
158
|
-
|
159
|
-
|
153
|
+
try {
|
154
|
+
const { data, status, headers } = dataOrPromise
|
155
|
+
const html = renderTemplate(template, location, { data })
|
156
|
+
res.status(status).set(headers).send(html)
|
157
|
+
} catch (error) {
|
158
|
+
serveInternalServerError(error, { req, res, next })
|
159
|
+
}
|
160
160
|
}
|
161
161
|
}
|
162
162
|
|
@@ -181,3 +181,12 @@ function renderTemplate(template, location, { data }) {
|
|
181
181
|
const html = template({ location, data })
|
182
182
|
return html
|
183
183
|
}
|
184
|
+
|
185
|
+
function serveInternalServerError(error, { res, req, next }) {
|
186
|
+
const response = res.status(500)
|
187
|
+
if (isProduction) {
|
188
|
+
findFile(req.path, internalServerError)
|
189
|
+
.then(file => file ? response.sendFile(file) : next())
|
190
|
+
.catch(next)
|
191
|
+
} else response.send(`<pre><title style='display: block;'>${error.stack || error.toString()}</title><pre>`)
|
192
|
+
}
|
package/package.json
CHANGED