我正在使用分别使用前端(AngularJS)和后端服务器(NodeJS,Express)的Web应用程序。
我们希望将所有网址重定向到“https://www”。
因此我们在前端找到了证书(可以使用)并启用了SSL。 然后我改变了我们的api调用常量,现在将请求发送到https://api.myapp.com/api/ ... 代替 http://api.myapp.com:PORT/api
然后我假设我们必须在我们的nodejs脚本的后端启用https。
所以我的问题是: 我需要另一张其他证书吗?我该如何在后端启用https?我只需要让服务器接受https请求吗?它是NPM模块吗?
现在,当我尝试向https://api.myapp.com/api/而不是http://api.myapp.com:PORT/api发送电话时,它会告诉我“net :: ERR_CONNECTION_REFUSED”
可能是因为我的nodejs没有处理https请求。
Https对我来说是新事物!
谢谢!
答案 0 :(得分:1)
首先,为了使用HTTPS,您需要使用 443端口,即您的https服务器 是听取该端口,您必须在URL中指定,否则net::ERR_CONNECTION_REFUSED
确认我的怀疑。
在express.js> = 3中,您需要使用以下内容:
var fs = require('fs');
var https = require('https');
var pKey = fs.readFileSync('sslcertificate.key', 'utf8');
var cert = fs.readFileSync('sslcertificate.crt', 'utf8');
var creds = {key: pKey , cert: cert };
var express = require('express');
var app = express();
var httpsServer = https.createServer(creds , app);
// Note the 443 port
httpsServer.listen(443);