简体   繁体   中英

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).

控制台截图

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.

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM