Verified Commit f85d0e3d authored by Aral Balkan's avatar Aral Balkan
Browse files

Closes #208: attempt to serve a single file fails with error

parent e022f396
......@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
## [15.4.0] - In progress…
This release implements a lot of small improvements, some of which have been longstanding.
### Improved
#### General:
......@@ -18,6 +20,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
Previously, the timeout was the default TCP connection timeout of 120 seconds which would make it look like the process had hanged. The new timeout is 5 seconds, which provides timely feedback.
- Displays a graceful error message if an attempt is made to serve a file instead of a directory (#208).
#### Documentation:
- Document initial run `@hostname` error on Mac with stale DNS cache (#138).
......
......@@ -246,7 +246,7 @@ function serve (args) {
} catch (error) {
if (error instanceof errors.InvalidPathToServeError) {
console.log(`\n ❌ ${clr('❨site.js❩ Error:', 'red')} The path to serve ${clr(options.path, 'yellow')} does not exist.\n`)
console.log(`\n ❌ ${clr(`❨site.js❩ Error:`, 'red')} ${error.message}\n`)
process.exit(1)
} else {
// Rethrow
......
......@@ -602,9 +602,19 @@ class Site {
// Middleware and routes that are unique to regular sites
// (not used on proxy servers).
async configureAppRoutes () {
// Ensure that the requested path to serve actually exists.
if (!fs.existsSync(this.absolutePathToServe)) {
throw new errors.InvalidPathToServeError(`Path ${this.pathToServe} does not exist.`)
let statusOfPathToServe
try {
statusOfPathToServe = fs.statSync(this.absolutePathToServe)
} catch (error) {
if (error.code === 'ENOENT') {
throw new errors.InvalidPathToServeError(`Path ${clr(this.pathToServe, 'yellow')} does not exist.`)
} else {
throw new errors.InvalidPathToServeError('Unexpected file system error', error)
}
}
if (statusOfPathToServe.isFile()) {
throw new errors.InvalidPathToServeError(`${clr(this.pathToServe, 'yellow')} is a file. Site.js can only serve directories.`)
}
// Async
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment