Home > database >  Trying to run express node js as https server but it won't run
Trying to run express node js as https server but it won't run

Time:12-03

I'm trying to get HTTPS working on express.js for node, and it won't run.

This is my server.js code.

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

const options = {
    pfx: fs.readFileSync('ssl/pfxfile.pfx'),
    passphrase: 'password'
};
const express = require('express');
const app = express();

const path = require('path');
    app.use(express.json());
    app.use(express.static("express"));
    app.use('/', function(req,res){
        res.sendFile(path.join(__dirname '/express/index.html'));
    });
 
var httpServer = http.createServer(app);
var httpsServer = https.createServer(options, app);

httpServer.listen(8080);
httpsServer.listen(8443);

When I run it reports no errors but it just get stuck to nothing (I waited 30 minutes to see if it does something and nothing happened).

Console Screenshot

CodePudding user response:

httpServer.listen(8080, ()=>{console.log('Server is running')}); If the server successfully started, it should output "Server is running" in the console. This is a nice way to check if the server is working as intended.

CodePudding user response:

I found my error, thanks for your answers, it's been helping me, my error was first that I didn't put any console.log and the second was that I was not typing 8443 in the browser.

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

const options = {
    pfx: fs.readFileSync('ssl/pfxfile.pfx'),
    passphrase: 'password'
};
const express = require('express');
const app = express();

const path = require('path');
    app.use(express.json());
    app.use(express.static("express"));
    app.use('/', function(req,res){
        res.sendFile(path.join(__dirname '/express/index.html'));
    });
 
const httpServer = http.createServer(app);
const port = process.env.PORT || 8080;
const httpsServer = https.createServer(options, app);
const portHttps = process.env.PORT || 8443;

httpServer.listen(port, () => console.log('Http listening on port '   port));
httpsServer.listen(portHttps, () => console.log('Https listening on port '   portHttps));
  • Related