diff --git a/ssl/README.md b/ssl/README.md new file mode 100644 index 0000000..17c040c --- /dev/null +++ b/ssl/README.md @@ -0,0 +1,42 @@ +# Quick Guide of SSL + +* Prerequisites + * [OpenSSL](https://www.openssl.org/) tool + * Openssl is preinstall on Linux / MacOS / Git Bash + * For windows, need to build the binary. Alternatively, use Git Bash (Install Git) +* Generate SSL key And cert + +```sh +$ openssl req -new -newkey rsa:2048 -nodes -keyout server.key -out server.csr + +// more sample +$ openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out certificate.pem +``` + + + +## Node JS implementation + +* Reference Code for secure server +* Place key and cert file in the `cert` folder of the nodejs project + +```js +const express = require('express'); +const https = require('https'); +const path = require('path'); +const fs = require('fs'); + +const app = express(); + +app.use('/', (req, res, next) => { + res.send('Hello from SSL Server'); +}); + +const sslServer = https.createServer({ + key: fs.readFileSync(path.join(__dirname, 'cert', 'server.key')), + cert: fs.readFileSync(path.join(__dirname, 'cert', 'server.csr)) +}, app); + +sslServer.listen(3443, () => console.log('Secure Server on port 3443')); +``` +