Skip to content

Commit 7a506df

Browse files
Merge pull request #157 from Web-Dev-Path/feat/functions-netlify
Creating blog function
2 parents b4e3022 + e0802a1 commit 7a506df

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

functions/blog.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//ISR (incremental static rendering) function that communicates to Netlify
2+
const fetch = require('node-fetch');
3+
4+
exports.handler = async (event, context) => {
5+
const res = await fetch('https://dev.to/api/articles?username=wdp');
6+
const posts = await res.json();
7+
8+
return {
9+
statusCode: 200,
10+
headers: { 'content-type': 'application/json' },
11+
body: JSON.stringify({
12+
posts: posts.map(post => ({
13+
title: post.title,
14+
image: post.cover_image,
15+
altTag: post.title,
16+
content: post.description,
17+
link: post.url,
18+
linkText: 'Read more',
19+
tagList: post.tag_list,
20+
})),
21+
}),
22+
};
23+
};

0 commit comments

Comments
 (0)