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 | */
|
9 | module.exports = function() {
|
10 |
|
11 | return (req, res, next) => {
|
12 |
|
13 | // Parse the URL
|
14 | const _url = url.parse(req.url)
|
15 |
|
16 | // Get the last char of the requested URL pathname
|
17 | const lastChar = _url.pathname.substr(-1)
|
18 |
|
19 | // Only continue when requested URL is a folder
|
20 | if (lastChar!=='/') return next()
|
21 |
|
22 | res.statusCode = 302
|
23 | res.setHeader('Location', `${ _url.pathname }index.html`)
|
24 | res.setHeader('Content-Length', '0')
|
25 | res.end()
|
26 |
|
27 | }
|
28 |
|
29 | } |
\ | No newline at end of file |