Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 573e590

Browse filesBrowse 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
Copy full SHA for 573e590

File tree

Expand file treeCollapse file tree

3 files changed

+22
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+22
-0
lines changed

‎pages/api/image.js

Copy file name to clipboard
+11Lines 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

Copy file name to clipboardExpand all lines: pages/index.js
+11Lines 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

Copy file name to clipboard
File renamed without changes.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.