How to configure Google Compute Engine to use HTTPS for nodejs server?

I want to run nodejs and socket.io server in Google Compute Engine using https / SSL.

I installed a self-signed certificate from https://cloud.google.com/compute/docs/load-balancing/http/ssl-certificates .

Now how to enable nodejs server to use https protocol?

Thanks,

+2
source share
2 answers

Below is the code I used for HTTPS in nodejs,

var app = require('express')();
var https = require('https');
var fs = require('fs');
var PORT = 443;

var options = {
  key: fs.readFileSync('XYZ.key'),
  cert: fs.readFileSync('ABC.crt')
};

var server = https.createServer(options, app).listen(PORT, function () {
  console.log("Express listening on port " + PORT);
});

// Post request.
var req_res = function (req, res) {
  console.log("[200] " + req.url);
  var fullBody = '';

  // Read post data.
  req.on('data', function (chunk) {
    fullBody += chunk.toString();

    if (fullBody.length > 1e6) {
      // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST
      req.connection.destroy();
    }
  });

  // Send response.
  req.on('end', function () {
    // empty 200 OK response for now
    res.writeHead(200, {
      'Content-Type': 'application/json'
    });
    res.end(JSON.stringify({
      'success': true
    }));
  });
};

// Hello World
app.get('/*', function (req, res) {
  res.status(200).send('Hello World...');
});

// Post request to receive notifications.
app.post('/post', req_res);

As for the Google Computing Engine, you just need to enable the 443-port port from the firewall.

gcloud compute firewall-rules create allow-https --description "https server" --allow tcp:443 --format json
+2
source

nodeJS HTTP . Nginx (http://nginx.org/en/docs/http/configuring_https_servers.html), https- 443 nginx. - proxy_pass Nginx NodeJS-. Nginx.

, https, nodeJS 80, , nodeJS , sudo ( ).

0

Source: https://habr.com/ru/post/1614742/


All Articles