当前位置: 首页 > 工具软件 > express.io > 使用案例 >

node.js https, express 用法 socket.io 跨域

江飞章
2023-12-01

In express.js (since version 3) you should use that syntax:

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

// your express configuration here

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

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

In that way you provide express middleware to the native http/https server

If you want your app running on ports below 1024, you will need to use sudo command (not recommended) or use a reverse proxy (e.g. nginx, haproxy).

包含socket.io
跨域:

var app = require('express')();
const { readFileSync } = require('fs');
const options = {
  key: readFileSync("./certs/xx.key", 'utf8'),
  cert: readFileSync("./certs/xx.crt", 'utf8'),
};
var http = require('https').Server(options, app);
var io = require('socket.io')(http,{
  cors: {
          origin: "*"
  }
});
 类似资料: