Skip to content
Advertisement

Trying to run express node js as https server but it won’t run

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement