tinyserve now knows the requested path is safe (resolveInRoot, from an earlier exercise, is
provided). Next question: what is there? A file, a folder, nothing? The answer decides the
HTTP status.
Write async resolveFile(root, urlPath) with
[`fs.stat`](https://nodejs.org/api/fs.html#fspromisesstatpath-options):
| Situation | Return |
|---|---|
resolveInRoot returns null | { status: 403 } |
nothing at that path (ENOENT), or a file used as a folder (ENOTDIR: /index.html/x) | { status: 404 } |
| a file | { status: 200, file, size } |
a folder, URL without trailing / | { status: 301, location: urlPath + '/' } |
a folder, URL with trailing /, containing a file index.html | { status: 200, file: '<folder>/index.html', size } |
a folder, URL with trailing /, no index.html file | { status: 403 } |
file is relative to root (path.relative(root, ...)), size is in bytes. Other errors
are thrown.
/docs to /docs/?A page served at /docs that links to guide.html sends the browser to /guide.html:
relative links resolve against the last /. Every static server redirects folders first.
// files: { 'docs/index.html': '<p>Docs</p>' }
await resolveFile(root, '/docs'); // { status: 301, location: '/docs/' }
await resolveFile(root, '/docs/'); // { status: 200, file: 'docs/index.html', size: 11 }Sample tests