-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathseo-audit.service.ts
34 lines (32 loc) · 999 Bytes
/
seo-audit.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Injectable } from '@nestjs/common';
import axios from 'axios';
import * as cheerio from 'cheerio';
@Injectable()
export class SeoAuditService {
async audit(url: string): Promise<any> {
try {
const { data } = await axios.get(url);
const $ = cheerio.load(data);
const title = $('title').text();
const metaDescription = $('meta[name="description"]').attr('content');
const metaKeywords = $('meta[name="keywords"]').attr('content');
const images = $('img').length;
const imagesWithAlt = $('img[alt]').length;
const links = $('a').length;
const internalLinks = $(`a[href^='${url}'], a[href^='/']`).length;
const externalLinks = links - internalLinks;
return {
title,
metaDescription,
metaKeywords,
images,
imagesWithAlt,
links,
internalLinks,
externalLinks,
};
} catch (error) {
throw new Error('Error fetching SEO audit data');
}
}
}