botpolice
v1.0.0
Published
Detects automated or crawler requests based on User-Agent heuristics.
Downloads
24
Maintainers
Readme
botpolice
Canonical URL:
https://alexstevovich.com/a/botpolice-nodejs
Software URL:
https://midnightcitylights.com/software/botpolice-nodejs
botpolice is a simple utility for detecting automated or crawler requests based on the User-Agent heuristics. It checks if the User-Agent string matches common bot names like "bot", "spider", "crawler", and others.
Installation
npm install botpoliceExample
import express from 'express';
import botpolice from 'botpolice';
const app = express();
// Use botpolice to block requests from bots
app.use((req, res, next) => {
if (botpolice(req)) {
res.status(403).send('Bot detected!');
} else {
next();
}
});
app.get('/', (req, res) => {
res.send('Hello, world!');
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});Function
botpolice(req)
req: The request object.- Returns:
trueif the request is from a bot,falseotherwise.
The function checks the User-Agent header in the request and matches it against common bot patterns (e.g., "bot", "spider", "crawler", etc.).
License
Licensed under the MIT License.
