I’m trying to get HTTPS working on express.js for node, and it won’t run.
This is my server.js
code.
JavaScript
x
24
24
1
const fs = require('fs');
2
const http = require ('http');
3
const https = require('https');
4
5
const options = {
6
pfx: fs.readFileSync('ssl/pfxfile.pfx'),
7
passphrase: 'password'
8
};
9
const express = require('express');
10
const app = express();
11
12
const path = require('path');
13
app.use(express.json());
14
app.use(express.static("express"));
15
app.use('/', function(req,res){
16
res.sendFile(path.join(__dirname+'/express/index.html'));
17
});
18
19
var httpServer = http.createServer(app);
20
var httpsServer = https.createServer(options, app);
21
22
httpServer.listen(8080);
23
httpsServer.listen(8443);
24
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).
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.