Skip to content

Commit

Permalink
v1.1.1 Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
daquinoaldo committed Sep 3, 2018
1 parent e8b8417 commit 560f3b6
Show file tree
Hide file tree
Showing 7 changed files with 1,414 additions and 99 deletions.
24 changes: 24 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"extends": "standard",
//"extends": "strongloop",
//"extends": "eslint:recommended",
"env": {
"node": true,
"es6": true,
"mocha": true
},
"parserOptions": {
"ecmaVersion": 8
},
"rules": {
"semi": ["warn", "never"],
"quotes": ["warn", "double"],
"comma-dangle": ["warn", "never"],
"space-before-function-paren": ["warn", "never"],
"curly": ["warn", "multi"],
"max-len": ["error", { "code": 80, "tabWidth": 2 }],
"quote-props": ["error", "as-needed"],
"no-console": "off",
"process.env": false
}
}
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ with two steps: (1) assert copyright on the software, and (2) offer
you this License which gives you legal permission to copy, distribute
and/or modify the software.

A secondary benefit of defending all users' freedom is that
A secondary benefit of defending all users" freedom is that
improvements made in alternate versions of the program, if they
receive widespread use, become available for other developers to
incorporate. Many developers of free software are heartened and
Expand Down Expand Up @@ -658,4 +658,4 @@ specific requirements.
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU AGPL, see
<http://www.gnu.org/licenses/>.
<http://www.gnu.org/licenses/>.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ Then just require this module, it will start the server automatically.

For example, put in your index.js file:
```
const app = require('https-localhost')
app.get('/', (req, res) => res.send('Hello World!'))
const app = require("https-localhost")
app.get("/", (req, res) => res.send("Hello World!"))
```

#### Specify the port
Expand Down
49 changes: 23 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,63 @@
#!/usr/bin/env node

const path = require('path')
const fs = require('fs')
const http = require('http')
const https = require('spdy') // using HTTP/2: spdy will be deprecated soon, waiting for HTTP/2 on https module.
const express = require('express')
const compression = require('compression')

// SSL CERTIFICATE
const path = require("path")
const fs = require("fs")
const http = require("http")
// using HTTP/2: spdy will be deprecated soon,
// waiting for HTTP/2 on https module.
const https = require("spdy")
const express = require("express")
const compression = require("compression")

// SSL certificate
const certOptions = {
key: fs.readFileSync(path.resolve(__dirname + "/cert/server.key")),
cert: fs.readFileSync(path.resolve(__dirname + "/cert/server.crt"))
key: fs.readFileSync(path.resolve(__dirname, "cert/server.key")),
cert: fs.readFileSync(path.resolve(__dirname, "cert/server.crt"))
}

const port = process.env.PORT || 443


// START THE APP

// run express
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 => {
app.server.on("connection", socket => {
const socketId = nextSocketId++
sockets[socketId] = socket
socket.on('close', () => delete sockets[socketId])
socket.on("close", () => delete sockets[socketId])
})

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

// redirect http to https
if (port === 443 || process.env.HTTP_PORT) {
app.http = http.createServer(function (req, res) {
res.writeHead(301, {"Location": "https://" + req.headers['host'] + req.url})
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 => {
app.http.on("connection", socket => {
const socketId = nextSocketId++
sockets[socketId] = socket
socket.on('close', () => delete sockets[socketId])
socket.on("close", () => delete sockets[socketId])
})
}

// SERVE STATIC FILES, if launched as: 'node index.js <static-path>'
if (require.main === module) { // called directly (not through require)
// 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()))
}

// ready
if (!process.env.TEST) console.info("Server running on port " + port + ".")


// CLOSE THE APP
// close the app
app.close = (callback) => {
const promises = [
new Promise(resolve => app.http.close(resolve)),
Expand All @@ -68,12 +66,11 @@ app.close = (callback) => {
// destroy all opens
for (const socketId in sockets)
sockets[socketId].destroy()

return Promise.all(promises).then(() => {
if (!process.env.TEST) console.info("Server closed.")
if (callback) callback()
})
}

// export as module
module.exports = app
module.exports = app
Loading

0 comments on commit 560f3b6

Please sign in to comment.