It has been successfully configured according to the tutorial
For the content of the previous part, refer to
https://blog.csdn.net/chenyufeng1991/article/details/60340006
The first half is to generate the certificate file. The key parts are excerpted as follows:
Use Linux to generate the following three files. This tool comes with Linux, and it is quite hard to find this tool under Windows.
openssl genrsa 1024 > private.pem
openssl req -new -key private.pem -out csr.pem
openssl x509 -req -days 365 -in csr.pem -signkey private.pem -out file.crt
After the files are generated, they are placed in a certificate folder. Note that they are in the same level as Express's app.js.
After that, add the following to the www file of Express:
var fs = require('fs');
var https = require('https');
var options = {
key : fs.readFileSync('./certificate/private.pem', 'utf8'),
cert : fs.readFileSync('./certificate/file.crt', 'utf8')
};
var SSLPORT = 443;
var httpsServer = https.createServer(options, app);
httpsServer.listen(SSLPORT);
httpsServer.on('error', onError);
httpsServer.on('listening', onListening);
The configuration is successful, and the complete www file is as follows:
#!/usr/bin/env node
/** * Module dependencies. */
var app = require('../app'); var debug = require('debug')('ltdweb:server'); var http = require('http'); var fs = require('fs');
//Add https support var https = require('https');
var STORAGE = require('../Storage'); var DEFINED = require('../DEFINED'); var CACHEVIEWS = require('../CacheViews');
STORAGE.InitUsers(); CACHEVIEWS.Initialize();
//Synchronously read key and signed certificate var options = { key : fs.readFileSync('./certificate/private.pem', 'utf8'), cert : fs.readFileSync('./certificate/file.crt' , 'utf8') };
/** * Get port from environment and store in Express. */
var port = normalizePort(process.env.PORT || '80'); app.set('port', port);
var SSLPORT = 443;
/** * Create HTTP server. */
var server = http.createServer(app); var httpsServer = https.createServer(options, app);
/** * Listen on provided port, on all network interfaces. */
server.listen(port); server.on('error', onError); server.on('listening', onListening);
httpsServer.listen(SSLPORT); httpsServer.on('error', onError); httpsServer.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); }