Skip to content

Commit b237cca

Browse files
committed
404 page like on gh pages
1 parent 4d5ecd6 commit b237cca

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

poops.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,22 @@ async function startServer() {
206206
await poops() // Initial compilation before starting the server
207207
const app = connect()
208208

209-
if (config.serve.base && pathExists(cwd, config.serve.base)) {
210-
app.use(serveStatic(path.join(cwd, config.serve.base)))
211-
} else {
212-
app.use(serveStatic(cwd))
213-
}
209+
const base = config.serve.base && pathExists(cwd, config.serve.base)
210+
? path.join(cwd, config.serve.base)
211+
: cwd
212+
213+
app.use(serveStatic(base))
214+
215+
// Serve 404.html for unmatched routes
216+
const notFoundPage = path.join(base, '404.html')
217+
app.use((req, res) => {
218+
res.statusCode = 404
219+
if (pathExists(notFoundPage)) {
220+
fs.createReadStream(notFoundPage).pipe(res)
221+
} else {
222+
res.end('Not Found')
223+
}
224+
})
214225

215226
let port = overridePort || config.serve.port || 4040
216227
if (!overridePort) port = await getAvailablePort(port, port + 10)

0 commit comments

Comments
 (0)