Skip to content

Commit

Permalink
Incremented coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
daquinoaldo committed Sep 11, 2018
1 parent ee95687 commit 2921cc4
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 41 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ or from another module with `process.env.PORT = <port-number>`.
If you specify a port number the http redirect to https will be disabled.
You can activate the http redirect again specifying not only the PORT environment variable but also the HTTP_PORT one.

#### Close the server
You can close the server with `app.close()`.
Closing the server is an async operation, you can get notified with a promise: `app.close().then(...)`.

---

### License
Expand Down
38 changes: 3 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,23 @@ const port = process.env.PORT || 443
const app = express()
app.server = https.createServer(certOptions, app).listen(port)

// save sockets for fast close
const sockets = []
let nextSocketId = 0
app.server.on("connection", socket => {
const socketId = nextSocketId++
sockets[socketId] = socket
socket.on("close", () => delete sockets[socketId])
})

// gzip compression and minify
app.use(compression())
app.set("json spaces", 0)

// redirect http to https
if (port === 443 || process.env.HTTP_PORT) {
if (port === 443 || process.env.HTTP_PORT)
app.http = http.createServer((req, res) => {
res.writeHead(301, { Location: "https://" + req.headers["host"] + req.url })
res.end()
}).listen(process.env.HTTP_PORT || 80)

app.http.on("connection", socket => {
const socketId = nextSocketId++
sockets[socketId] = socket
socket.on("close", () => delete sockets[socketId])
})
}

// serve static files, if launched as: "node index.js <static-path>"
if (require.main === module) {
const staticPath = process.argv[2]
app.use(express.static(staticPath || process.cwd()))
}
if (require.main === module)
app.use(express.static(process.argv[2] || process.cwd()))

// ready
console.info("Server running on port " + port + ".")

// close the app
app.close = () => {
const promises = [
new Promise(resolve => app.http.close(resolve)),
new Promise(resolve => app.server.close(resolve))
]
// destroy all opens
for (const socketId in sockets)
sockets[socketId].destroy()
return Promise.all(promises).then(() => {
console.info("Server closed.")
})
}

// export as module
module.exports = app
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"pretest": "eslint --ignore-path .gitignore .",
"test": "mocha --exit",
"posttest": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -R spec ./test/*.js",
"posttest": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -R spec ./test/*.js --exit",
"start": "node index.js"
},
"bin": {
Expand Down
3 changes: 2 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ describe("Testing https-localhost", () => {
.then(res => assert(res.data === "TEST"))
})
it("serve static file used as standalone tool", async function() {
await app.close()
process.env.PORT = 4444
process.env.HTTP_PORT = 8081
runScript("index.js", ["test"], true)
.then(process => proc = process) // eslint-disable-line no-return-assign
.catch(err => console.error(err))
Expand Down

0 comments on commit 2921cc4

Please sign in to comment.