-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split server into its own file (#224)
- Loading branch information
1 parent
6e49545
commit 6f8d65a
Showing
2 changed files
with
55 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as http from 'http'; | ||
import * as path from 'path'; | ||
import * as util from 'util'; | ||
import * as fs from 'fs'; | ||
import * as marked from 'marked'; | ||
import finalhandler = require('finalhandler'); | ||
import serveStatic = require('serve-static'); | ||
import enableDestroy = require('server-destroy'); | ||
|
||
const readFile = util.promisify(fs.readFile); | ||
|
||
/** | ||
* Spin up a local HTTP server to serve static requests from disk | ||
* @param root The local path that should be mounted as a static web server | ||
* @param port The port on which to start the local web server | ||
* @param markdown If markdown should be automatically compiled and served | ||
* @private | ||
* @returns Promise that resolves with the instance of the HTTP server | ||
*/ | ||
export async function startWebServer( | ||
root: string, | ||
port: number, | ||
markdown?: boolean | ||
) { | ||
return new Promise<http.Server>((resolve, reject) => { | ||
const serve = serveStatic(root); | ||
const server = http | ||
.createServer(async (req, res) => { | ||
const pathParts = req.url!.split('/').filter(x => !!x); | ||
if (pathParts.length > 0) { | ||
const ext = path.extname(pathParts[pathParts.length - 1]); | ||
if (markdown && ext.toLowerCase() === '.md') { | ||
const filePath = path.join(path.resolve(root), req.url!); | ||
const data = await readFile(filePath, {encoding: 'utf-8'}); | ||
const result = marked(data, {gfm: true}); | ||
res.writeHead(200, { | ||
'content-type': 'text/html', | ||
}); | ||
res.end(result); | ||
return; | ||
} | ||
} | ||
return serve(req, res, finalhandler(req, res) as () => void); | ||
}) | ||
.listen(port, () => resolve(server)) | ||
.on('error', reject); | ||
enableDestroy(server); | ||
}); | ||
} |