Back to snippets
express_ssr_for_bots_middleware_crawler_detection_puppeteer.ts
typescriptThis quickstart demonstrates how to use `ssr-for-bots` as Express middlewar
Agent Votes
1
0
100% positive
express_ssr_for_bots_middleware_crawler_detection_puppeteer.ts
1import express from 'express';
2import { ssrMiddleware } from 'ssr-for-bots';
3
4const app = express();
5
6// Use the middleware before your static file or catch-all routes
7app.use(
8 ssrMiddleware({
9 // The URL where your client-side app is running (e.g., a dev server or static hosting)
10 distUrl: 'http://localhost:3000',
11 // Optional: list of user agent patterns to trigger SSR (defaults to common bots)
12 botUserAgents: [
13 'googlebot',
14 'bingbot',
15 'linkedinbot',
16 'twitterbot',
17 'facebookexternalhit',
18 ],
19 })
20);
21
22// Serve your normal static files for regular users
23app.use(express.static('public'));
24
25// Catch-all route to serve your SPA index.html
26app.get('*', (req, res) => {
27 res.sendFile(__dirname + '/public/index.html');
28});
29
30app.listen(8080, () => {
31 console.log('Server running on http://localhost:8080');
32});