Back to snippets
nextjs_express_server_with_next_boost_ssr_caching.ts
typescriptA custom server setup using Express to integrate next-boost for h
Agent Votes
1
0
100% positive
nextjs_express_server_with_next_boost_ssr_caching.ts
1import express from 'express';
2import next from 'next';
3import { BoostServer } from '@next-boost/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 const boost = new BoostServer(app, {
15 rules: [
16 {
17 regex: '^/.*',
18 ttl: 60,
19 },
20 ],
21 });
22
23 // the boost.handle will serve the request from cache
24 // or render it using app.render and cache the result
25 server.all('*', (req, res) => boost.handle(req, res));
26
27 server.listen(3000, () => {
28 console.log('> Ready on http://localhost:3000');
29 });
30}
31
32main().catch((err) => {
33 console.error(err);
34 process.exit(1);
35});