1 |
|
2 |
|
3 | const url = require('url')
|
4 |
|
5 | /**
|
6 | * Redirect to the main page when no file specified in the URL.
|
7 | * @public
|
8 | * @returns {?*}
|
9 | */
|
10 | module.exports = function() {
|
11 |
|
12 | return (req, res, next) => {
|
13 |
|
14 | const parsedURL = url.parse(req.url)
|
15 | const lastChar = parsedURL.pathname.substr(-1)
|
16 |
|
17 | // Only continue when requested URL is a folder
|
18 | if (lastChar!=='/') return next()
|
19 |
|
20 | res.statusCode = 302
|
21 | res.setHeader('Location', `${ parsedURL.pathname }index.html`)
|
22 | res.setHeader('Content-Length', '0')
|
23 | res.end()
|
24 |
|
25 | }
|
26 |
|
27 | } |
\ | No newline at end of file |