Skip to content

Commit 573e590

Browse files
committed
Add API endpoint that renders an image
We can return an image from an API endpoint. The image must be passed as a buffer.
1 parent 03b9939 commit 573e590

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

pages/api/image.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import rick from "buffer-loader!../../rick.jpg";
2+
3+
export default (req, res) => {
4+
res.status(200);
5+
res.setHeader("Content-Type", "image/jpeg");
6+
// Send the image buffer. There are many other ways to send images and other
7+
// files. For example, you can create a buffer from a base64-encoded string
8+
// of an image: https://stackoverflow.com/a/28440633/6451879
9+
// res.send(Buffer.from(%BASE64-STRING%, "base64"))
10+
res.send(rick);
11+
};

pages/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ const Index = () => (
135135
with content-type XML
136136
</a>
137137
</li>
138+
<li>
139+
<a href='/api/image'>
140+
with image response
141+
</a>
142+
{' '}
143+
(
144+
<a href='https://github.com/FinnWoelm/next-on-netlify-demo/tree/master/pages/api/image.js'>
145+
code
146+
</a>
147+
)
148+
</li>
138149
</ul>
139150

140151
<h2>Preview Mode</h2>

roll.jpg renamed to rick.jpg

File renamed without changes.

0 commit comments

Comments
 (0)