-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for port scanning in the development server to use …
…the first available port from 9999 to 10999. (#145)
- Loading branch information
1 parent
338779f
commit 7de482d
Showing
4 changed files
with
35 additions
and
21 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
This file was deleted.
Oops, something went wrong.
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,32 @@ | ||
const http = require('http'); | ||
const portscanner = require('portscanner'); | ||
const static = require('node-static'); | ||
|
||
const files = new static.Server(process.cwd(), {cache: false}); | ||
|
||
const server = http.createServer((request, response) => { | ||
response.setHeader('Cache-Control', 'no-cache,must-revalidate'); | ||
|
||
request.addListener('end', () => { | ||
files.serve(request, response, (err) => { | ||
if (err) { | ||
response.writeHead(err.status, err.headers); | ||
response.end('Not Found'); | ||
} | ||
|
||
console.log([ | ||
(new Date()).toISOString(), | ||
`[${response.statusCode}]`, | ||
request.url | ||
].join(' ')); | ||
}); | ||
}).resume(); | ||
}); | ||
|
||
portscanner.findAPortNotInUse(9999, 10999).then((port) => { | ||
server.listen(port, '0.0.0.0'); | ||
console.log(`serving "." at http://0.0.0.0:${port}`); | ||
}).catch((err) => { | ||
console.log('could not find an open port: ', err); | ||
process.exit(1); | ||
}); |