-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (41 loc) · 1.3 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
let express = require('express')
let bodyParser = require('body-parser')
let tmp = require('tmp')
let fs = require('fs')
let fsp = require('fs-promise')
let archiver = require('archiver')
let pem = require('pem')
let app = express()
let urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(express.static('public'))
app.get('/', urlencodedParser, (req, res) => {
pem.createCertificate({days:1, selfSigned:true}, (err, keys) => {
let certFile = tmp.fileSync()
let keyFile = tmp.fileSync()
fsp.writeFile(certFile.fd, keys.certificate).then((err) => {
if (err) {
console.log(err)
}
fsp.writeFile(keyFile.fd, keys.serviceKey).then((err) => {
if (err) {
console.log(err)
}
let zip = archiver.create('zip', {})
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-disposition': 'attachment; filename=ssl.zip'
});
zip.pipe(res);
zip
.append(fs.readFileSync(certFile.name), {name: 'ssl/server.crt'})
.append(fs.readFileSync(keyFile.name), {name: 'ssl/server.key'})
.finalize()
})
})
})
})
app.use(function(err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
})
app.listen(process.env.PORT)