I
I
ince2020-03-01 14:27:23
Node.js
ince, 2020-03-01 14:27:23

How to make https on the server?

There is such server, on the 80th port on http worked well.
Added https on port 443 and stopped working.
There are no errors in the console - the server says that it has successfully started listening on port 443.
But it doesn't go to my site.
What can be done?

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('server:server');
var http = require('http');
var https = require('https');
var fs = require('fs');

/**
 * Get port from environment and store in Express.
 */
console.log('process.env.NODE_ENV')
console.log(process.env.NODE_ENV)
let port = (process.env.NODE_ENV === 'prod')? (process.env.PORT || '443') : '3000';
port = normalizePort(port);
app.set('port', port);

/**
 * Create HTTP server.
 */
let server
if (process.env.NODE_ENV === 'prod'){
  const options = {
    key: fs.readFileSync('keys/private.pem'),
    cert: fs.readFileSync('keys/cert.pem'),
    ca: fs.readFileSync('keys/rootcert.key'),
    requestCert: true,
    rejectUnauthorized: false,
  };
  server = https.createServer(options, app);
} else {
  server = http.createServer(app);
}
/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port, () => console.log(`Started on port ${port}`));
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2020-03-01
@ince

What can be done?

do not suffer from garbage and use cloudflare flexible
what for do you sculpt the ssl on the server if the cloud doesn't care?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question