You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This Library is an ES6 version of the original PHP class @CrawlerDetect, it helps you detect bots/crawlers and spiders only by scanning the user-agent string or from the global request.headers.
Installation
npm install es6-crawler-detect
Usage
ECMAScript 6 (ES6)
'use strict';constexpress=require('express')const{ Crawler, middleware }=require('es6-crawler-detect')constapp=express()app.get('your/route',functionasync(request,response){// create a new Crawler instancevarCrawlerDetector=newCrawler(request)// check the current visitor's useragentif(CrawlerDetector.isCrawler()){// true if crawler user agent detected}// or check a user agent stringif(CrawlerDetector.isCrawler('Mozilla/5.0 (compatible; Sosospider/2.0; +https://help.soso.com/webspider.htm)')){// true if crawler user agent detected}// Output the name of the bot that matched (if any)response.send(CrawlerDetector.getMatches())})/** * Or by using the middleware*/app.use(middleware((request,reponse)=>{// do something here// e.g. console.log(request.body)// e.g. return response.status(403).send('Forbidden')}))app.get('/crawler',functionasync(request,response){// or check a user agent stringrequest.Crawler.isCrawler('Mozilla/5.0 (compatible; Sosospider/2.0; +https://help.soso.com/webspider.htm)')// Output the name of the bot that matched (if any)response.send(request.Crawler.getMatches())})
TypeScript
import{Crawler,middleware}from'es6-crawler-detect'constCrawlerDetector=newCrawler()// check the current visitor's useragentif(CrawlerDetector.isCrawler()){// true if crawler user agent detected}// or check a user agent stringif(CrawlerDetector.isCrawler('Mozilla/5.0 (compatible; Sosospider/2.0; +https://help.soso.com/webspider.htm)')){// true if crawler user agent detected}// Output the name of the bot that matched (if any)console.log(CrawlerDetector.getMatches())/** * Or by using the middleware*/middleware((request,reponse)=>{// do something here// e.g. console.log(request.body)// e.g. return response.status(403).send('Forbidden')})
Contributing
If you find a bot/spider/crawler user agent that CrawlerDetect fails to detect, please submit a pull request with the regex pattern added to the data array in ./crawler/crawlers.ts.
About
🕷️ This is an ES6 adaptation of the original PHP library CrawlerDetect, this library will help you detect bots/crawlers/spiders vie the useragent.