How to Use HTTP2 in Loopback 4 Applications?

Search for a command to run...

No comments yet. Be the first to comment.
(...and used for years without realizing!)

Database connection pooling is a technique used to improve the performance of applications that access a database. Connection pooling allows an application to reuse existing database connections, instead of creating a new connection each time it need...

As web applications continue to grow in popularity, the need for secure API endpoints becomes increasingly important.One mechanism that is often used to protect these endpoints is Cross-Origin Resource Sharing (CORS). However, it is important to unde...

AWS S3 lifecycle configuration allows users to automatically manage the storage and deletion of objects in their S3 buckets. By creating and applying lifecycle rules, users can ensure that their data is stored in the most cost-effective way possible,...

Next.js is a popular framework for building server-rendered React applications. It provides a powerful set of tools and features that make it easy to create high-performance and SEO-friendly web applications. In this article, we will explore some tip...

HTTP/2 is a major revision of the HTTP network protocol mainly in highlights because it makes our applications faster. Tech giants including Google, Netflix, Twitter already use this protocol.
In this post, I'll be showing how you can utilize spdy npm package to run loopback applications on this protocol.
Here's what you have to do in your existing app:
npm i spdy
index.tsChange 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.tsfile 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 :)