如何在Loopback 4应用程序中使用HTTP2?

发布于 2025-02-12 14:26:00 字数 94 浏览 3 评论 0原文

我们计划在Loopback 4应用程序中实现HTTP2。我们使用过HTTP,套接字服务器,但从未使用过HTTP2。

在我的应用程序中使用HTTP2的过程是什么?

We are planning to implement http2 in a loopback 4 application. We had used http, socket servers but never http2.

What is the procedure to use http2 in my application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒中人 2025-02-19 14:26:00

这是您在现有应用中必须要做的:

步骤1:安装 spdy spdy

npm i spdy

步骤2:配置index.ts

更改您的主函数:

import spdy from "spdy";

export async function main(options: ApplicationConfig = {}) {
  
  // specify cert and key file paths for SSL
  const serverOptions: spdy.ServerOptions = {
    key: fs.readFileSync(
      path.join(__dirname, '..', 'keys', 'localhost-privkey.pem'),
    ),
    cert: fs.readFileSync(
      path.join(__dirname, '..', 'keys', 'localhost-cert.pem'),
    ),
  };

  // setting listenOnStart to false will not start the default httpServer
  options.rest.listenOnStart = false;

  // Replace YourApplication with your class
  const app = new YourApplication(options);
  await app.boot();
  await app.start();

  // create server
  const server = spdy.createServer(serverOptions, app.requestHandler);

  // to avoid process exit on warnings
  server.on('warning', console.warn);

  server.listen(3000, () => {
    console.log('Listening on https://localhost:3000/');
  });

  return app;
}

src/index.ts中 防止默认的HTTP服务器使用loopback的请求处理程序app.requesthandler使用SPDY启动并启动服务器,该将用于所有传入请求。

查看此 pastebin 包含整个index.ts.ts.ts.ts更改。

要生成本地主机的证书和键:

openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
  -keyout localhost-privkey.pem -out localhost-cert.pem

您可能需要在Chrome中允许自签名证书以及/explorer按预期工作。

就是这样,您现在可以运行应用程序,并享受http2的功能:)

博客文章: https://shubham-web.medium.com/how-to-use-http2-in-loopback-4-applications-5e8381c7b38

Here's what you have to do in your existing app:

Step 1: Install spdy

npm i spdy

Step 2: Configure index.ts

Change your main function in src/index.ts with this:

import spdy from "spdy";

export async function main(options: ApplicationConfig = {}) {
  
  // specify cert and key file paths for SSL
  const serverOptions: spdy.ServerOptions = {
    key: fs.readFileSync(
      path.join(__dirname, '..', 'keys', 'localhost-privkey.pem'),
    ),
    cert: fs.readFileSync(
      path.join(__dirname, '..', 'keys', 'localhost-cert.pem'),
    ),
  };

  // setting listenOnStart to false will not start the default httpServer
  options.rest.listenOnStart = false;

  // Replace YourApplication with your class
  const app = new YourApplication(options);
  await app.boot();
  await app.start();

  // create server
  const server = spdy.createServer(serverOptions, app.requestHandler);

  // to avoid process exit on warnings
  server.on('warning', console.warn);

  server.listen(3000, () => {
    console.log('Listening on https://localhost:3000/');
  });

  return app;
}

All we're doing in the above code is, preventing the default http server from being started and starting the server using spdy with loopback's request handler app.requestHandler that will be used for all incoming request.

Check out this pastebin containing entire index.ts file content after the changes.

To generate certificate and keys for localhost use:

openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
  -keyout localhost-privkey.pem -out localhost-cert.pem

You may need to allow self-signed certificates in Chrome as well for /explorer to work as expected.

And that's it, you can now run your app, and enjoy the power of http2 :)

Blog Post: https://shubham-web.medium.com/how-to-use-http2-in-loopback-4-applications-5e83881c7b38

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文