简体   繁体   中英

https on node.js server running on port 3000

So I have a node.js server like http://someIp:3000 . Is it possible to install https on this without getting a domain name and running on port 80? The reason I'm asking this question is that I have a php website running on https and when I try to communicate with my node server it complains that you requested an insecure XMLHttpRequest endpoint.

Yes it is:

var fs = require('fs');
var https = require('https');
var express = require('express');
var socket = require('socket.io');
var storage = require('node-persist');
var port = 8443;

var sslOptions = {
  pfx: fs.readFileSync('cert/cert.pfx'),
     passphrase: "Node2015"
};

var app = express();

app.all('/*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, *');
  next();
});

var server = https.createServer(sslOptions, app);

You should either configure the webserver running PHP to connect to your NodeJS Server using a reverse proxy (then both will be accessible through the same hostname and port) or configure NodeJS with the correct CORS headers.

I think in your scenario the reverse proxy will be easier to configure. Configure NodeJS to only allow localhost https connections and then configure your Apache to proxy requests coming in.

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