【问题标题】:how to make my node.js server https instead of http如何使我的 node.js 服务器 https 而不是 http
【发布时间】:2021-10-07 20:34:27
【问题描述】:

大家好!

我是后端开发的新手,我正在尝试使用 typescript 设置 node.js 服务器。

我遵循了一个教程,它似乎有效!

但在开始设置和编码我的所有路由和请求之前,我想使用 https 而不是 http,这是我从未做过的事情。 (在我的学校项目中,这不是强制性的)。

这是我的 app.ts,我在其中设置了所有内容。

import dotenv from 'dotenv';
import express from 'express';
import MasterRouter from './routers/MasterRouter';



// load the environment variables from the .env file
dotenv.config({
  path: '.env'
});

class Server {
    public app = express();
    public router = MasterRouter;
}

// initialize server app
const server = new Server();

// make server app handle any route starting with '/api'
server.app.use('/api', server.router);


// make server listen on some port
((port = process.env.APP_PORT || 5000) => {
  server.app.listen(port, () => console.log(`> Listening on port ${port}`));
})();

你们能给我一些关于如何拥有安全服务器的建议吗?

我已经得到了文件cert.pemkey.pem

谢谢!

【问题讨论】:

    标签: node.js typescript https backend router


    【解决方案1】:

    HTTPS 本地开发并不常见。您是如何颁发证书的以及针对哪些域?浏览器会将您的证书识别为来自 SSL 证书颁发机构吗?尽管您的意图是最好的,但您可能仍然会在浏览器中看到不安全的连接屏幕。

    当今一个流行的选择是使用HTTP 完成所有开发工作。然后,您在服务器前使用 Cloudflare 等反向代理或 AWS 负载均衡器。然后,您的用户连接并与代理协商 SSL 连接,代理将通过 HTTP 与您的服务器通信。

    设置起来很容易。您只需允许 Cloudflare 管理您的 DNS,它会在将其传递到您的节点应用程序之前自动通过其服务器通过 SSL 路由所有连接。这将为您的网站提供漂亮的安全锁图标。

    了解更多后,您还可以加密 Cloudflare 和您的服务器之间的流量。

    【讨论】:

    • 非常感谢!我想我会在开发阶段结束时使用 certBot
    猜你喜欢
    • 2019-02-09
    • 1970-01-01
    • 2018-02-01
    • 2021-10-10
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多