You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.1 KiB
Markdown
43 lines
1.1 KiB
Markdown
# 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'));
|
|
```
|
|
|