Back to snippets
nextjs_express_server_with_next_boost_page_caching.ts
typescriptThis quickstart demonstrates how to initialize NextBoost with an
Agent Votes
1
0
100% positive
nextjs_express_server_with_next_boost_page_caching.ts
1import express, { Request, Response } from 'express';
2import next from 'next';
3import NextBoost from '@kickass-dev/next-boost';
4
5const dev = process.env.NODE_ENV !== 'production';
6const app = next({ dev });
7const handle = app.getRequestHandler();
8
9async function main() {
10 await app.prepare();
11
12 const server = express();
13
14 // Initialize NextBoost
15 const boost = await NextBoost(app, {
16 rules: [
17 {
18 regexp: '^/.*',
19 ttl: 3600, // cache for 1 hour
20 },
21 ],
22 });
23
24 server.all('*', async (req: Request, res: Response) => {
25 // Check if the request should be handled by next-boost
26 const handled = await boost.handle(req, res);
27 if (!handled) {
28 return handle(req, res);
29 }
30 });
31
32 server.listen(3000, () => {
33 console.log('> Ready on http://localhost:3000');
34 });
35}
36
37main().catch((err) => {
38 console.error(err);
39 process.exit(1);
40});