i'm using https server with express in my app, the problem is that express never listens on that port, it works with http but not with https
app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(function (error, req, res, next) {
if (error instanceof SyntaxError) {
res.status(400).json({
status: 'failed',
message: "Enter a valid JSON object."
});
} else {
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
next();
}
});
app.use(cors());
app.use(morgan('dev'));
require('./config/routes.js')(app);
server = https.createServer({
key: fs.readFileSync(process.env.SSL_LOCATION+"ssl.key"),
cert: fs.readFileSync(process.env.SSL_LOCATION+"ssl.cert")
},app).listen(parseInt(process.env.SOCKET_PORT, 10) || 4000, '0.0.0.0',function(){
console.log('Express server listening to port '+parseInt(process.env.SOCKET_PORT, 10) || 4000);
});
io = require('socket.io').listen(server);
but if i add
port=5455
app.listen(port);
express starts listening but on different port any suggestions?
Try to replace your port with a constant number and test it. Also, try to run my code below (it is based on yours and works for me). Write if it doesn't solve your problem.
const https = require('https');
const express = require("express");
const fs = require('fs');
const bodyParser = require("body-parser");
const morgan = require('morgan');
app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(morgan('dev'));
// for test
app.get('/', (req, res) => {
res.send("Work");
})
const server = https.createServer({
key: fs.readFileSync("./ssl.key"),
cert: fs.readFileSync("./ssl.crt")
},app)
server.listen(8000)
io = require('socket.io').listen(server);
console.log('Start');
console.log(server.address().port);
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.