Skip to content

Commit

Permalink
Update how-to-create-a-HTTPS-server.md (nodejs#2203)
Browse files Browse the repository at this point in the history
In accordance with issue nodejs#1977, I have updated the Article how-to-access-query-string-parameters with the following changes:

1. Used const instead of var.
2. Removed unnecessary server variable.
3. Added a note on how to execute the program.
  • Loading branch information
apal21 authored and ZYSzys committed Apr 24, 2019
1 parent 4c47e8b commit 8216eeb
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions locale/en/knowledge/HTTP/servers/how-to-create-a-HTTPS-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,25 @@ To generate a self-signed certificate, run the following in your shell:
openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.pem
rm csr.pem

This should leave you with two files, `cert.pem` (the certificate) and `key.pem` (the private key). This is all you need for a SSL connection. So now you set up a quick hello world example (the biggest difference between https and [http](/how-do-i-create-a-http-server) is the `options` parameter):
This should leave you with two files, `cert.pem` (the certificate) and `key.pem` (the private key). Put these files in the same directory as your Node.js server file. This is all you need for a SSL connection. So now you set up a quick hello world example (the biggest difference between https and [http](/how-do-i-create-a-http-server) is the `options` parameter):

var https = require('https');
var fs = require('fs');
const https = require('https');
const fs = require('fs');

var options = {
const options = {
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem')
};

var a = https.createServer(options, function (req, res) {
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("hello world\n");
}).listen(8000);

NODE PRO TIP: Note `fs.readFileSync` - unlike `fs.readFile`, `fs.readFileSync` will block the entire process until it completes. In situations like this - loading vital configuration data - the `sync` functions are okay. In a busy server, however, using a synchronous function during a request will force the server to deal with the requests one by one!

> To start your https server, run `node app.js` (here, app.js is name of the file) on the terminal.
Now that your server is set up and started, you should be able to get the file with curl:

curl -k https://localhost:8000
Expand Down

0 comments on commit 8216eeb

Please sign in to comment.