How to create a https interface with express?

const app = express();
let keywords
app.get("/api/getNews", (req, res) => {
    keywords = req.query.keywords
    console.log(keywords)
    getNewsFromSql(function () {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
        res.send(resp)//
    });
});
const server = app.listen(3000, function () {
    const host = server.address().address;
    const port = server.address().port;
    console.log("Example server listening at http://%s:%s", host, port)
})

currently, you can listen to the get request of http on port 3000 and check the library.
is that the front end may run on the https server, and the browser ajax refuses to request the interface of http. The purpose of
is to listen for https request.
now the server already has a free certificate from Symantec. Online tutorials basically create self-signed certificates through openssl.
how to create a https interface with existing certificates in express?


the configuration is basically the same, as long as you replace the corresponding self-signed certificate with your free certificate.

var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey  = fs.readFileSync('sslcert/server.key', 'utf8'); //
var certificate = fs.readFileSync('sslcert/server.crt', 'utf8'); //

var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();

// express 

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

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