Node.js HTTPS Secure Error

后端 未结 3 490
逝去的感伤
逝去的感伤 2020-12-14 03:30

I am trying to create a secure node.js server to use with my site that is using ssl (https).

const crypto = require(\'crypto\'),
      fs = require(\"fs\"),
         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 03:52

    I have worked on the https secure with the ssl here is the working code for making the https and http

    var fs = require('fs');
    var http = require('http');
    var https = require('https');
    var debug = require('debug')('expressapp');
    var app = require('../app');
    var CONSTANTS = require('../config/CONSTANTS.js');
    var AWS = require('aws-sdk');
    var certificate =fs.readFileSync('ssl/server.crt',{encoding:'utf8'},function(err, data ) {
      console.log( data );});
    var privateKey  = fs.readFileSync('ssl/server.key',{encoding:'utf8'},function(err, data ) {
      console.log( data );});
    
    
    var credentials = {
      key: privateKey,
      cert: certificate,
      rejectUnauthorized:false
    };
    
    // UNCOMMENT THIS LINE AFTER INSTALLING CA CERTIFICATE
     //credentials.ca = fs.readFileSync('ssl/server.crt', 'utf8');;
    
    var httpServer = http.createServer(app);
    var httpsServer = https.createServer(credentials, app);
    
      httpServer.listen(CONSTANTS.PORT.HTTP, function() {
        console.log('HTTP server listening on port ' + CONSTANTS.PORT.HTTP);
       })  ;
    
    httpsServer.listen(CONSTANTS.PORT.HTTPS, function() {
      console.log('HTTPS server listening on port ' + CONSTANTS.PORT.HTTPS);
    });

提交回复
热议问题