"use client"
import axios from "axios";
import { useState } from "react";
export default async function getImage({params}) {
[imageData, setImageData] = useState(null)
const getImage = async () => {
const response = await axios.get(`http://localhost:5000/api/image/${params.hash}`)
setImageData(response.data)
}
getImage()
// console.log(val.data)
if(imageData) {return (val.data)}
}
``` I am unsure how to do I make this work. I am displaying image data to be reference by another component using markdown as such

#unsure how to integrate expressjs backend, aws api gateway, and nextjs.
216 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
its a blob
ok and you want to display the image? Or what would you like to do?
I want to display the image using markdown by referencing as such : ![image name] (/api/image/hash)
you can just replace the path with your link like:
->
![image name] (/api/image/hash)
How do I make the call to the backend that gives it the blob?
normally the library that you using can also replace the markdown like that
I don't have an issue with the markdown.
I have an issue with rendering the blob or like retrieving it from the backend
blob:yourexternalurl
normally you are able to generate a url like that and then be able to display this url
const getAddress = (fieldName) =>{
const imageHash = imageUrls[fieldName]
return /api/image/${imageHash}
}
this is how I generate the URL
I don't have an issue with url generation
🤔
i just want the right resource to be available at that url,which is image data
I was pulling the data from the backend, but I face an error
Unhandled Runtime Error
Error: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding 'use client' to a module that was originally written for the server.
hm I think I know not enought to help you here. Maybe someone else is able to help there
@spare crescent could you help me out here?
you need to fetch the data inside a useEffect if you are using client component
export default async function getImage({ params }) {
const [imageData, setImageData] = useState(null);
useEffect(() => {
const getImage = async () => {
const response = await axios.get(
`http://localhost:5000/api/image/${params.hash}`
);
setImageData(response.data);
};
getImage();
}, []);
if (!imageData) {
return <div>loading...</div>
}
return <div></div>
}
@spare crescent that did not work
"use client"
import axios from "axios";
import { useState, useEffect } from "react";
export default function getImage({params}) {
const [imageData, setImageData] = useState(null)
useEffect( () =>{
const getImage = async () => {
const response = await axios.get(`http://localhost:5000/api/image/${params.hash}`)
setImageData(response.data)
};
getImage();
}, [])
return ({imageData})
}
what is imageData
I removed the async for the function name and that solved the issue partially
image data is the raw image data
Error: Objects are not valid as a React child (found: object with keys {data}). If you meant to render a collection of children, use an array instead.
I want image data to be found at the path: /api/image/:hash
"use client"
import axios from "axios";
import { useState, useEffect } from "react";
export default function getImage({params}) {
const [imageData, setImageData] = useState(null)
useEffect( () =>{
const getImage = async () => {
const response = await axios.get(`http://localhost:5000/api/image/${params.hash}`)
setImageData(response.data)
console.log(response.data)
};
getImage();
}, [])
return <div></div>
}
show me the console.log
"use client"
import axios from "axios";
import { useState, useEffect, useRef } from "react";
export default function getImage({params}) {
const [loaded, setLoaded] = useState(false)
const ref = useRef<HTMLImageElement>(null)
useEffect( () =>{
const getImage = async () => {
const response = await axios.get(`http://localhost:5000/api/image/${params.hash}`)
const blob = await response.blob()
const url = URL.createObjectURL(blob)
if (ref.current) {
ref.current.src = url
setLoaded(true)
}
};
getImage();
}, [])
if (!loaded) return null
return <img ref={ref} />
}
edited
that would display an image. However, will I be able to reference it elsewhere in my app?
does that make sense?
wdym?
i am using markdown in certain aspects of my blog site
So the client should be able to reference the image as I have mentioned it and place the image where the image tag is created
this only renders the image at the path /api/image/:hash
can i reference it another place such as <img url ="/api/image/:hash" />
will the reference work?
you don't want to show this link http://localhost:5000/api/image/${params.hash}?
I could do that
then I think you could just use that link for the image in markdown?
I guess so
you could create a route handler if you want to use this link![image name] (/api/image/hash)
// app/api/image/[hash]/route.ts
export async function GET(req:Request, { params }: { params: { hash: string } }) {
return fetch("http://localhost:5000/api/image/${params.hash}")
}
does it work for you?
I am still testing one minut
It is returning the response object
the object has the following structure: {data: image data}
// app/api/image/[hash]/route.ts
export async function GET(req:Request, { params }: { params: { hash: string } }) {
const response = await fetch("http://localhost:5000/api/image/${params.hash}")
return response.data
}
await fetch()
However, this is no longer a request
you didnt await
it worked without the await
I mean I could alter my api to just return without data
I updated my api. let me test my referencing it in markdwon
// app/api/image/[hash]/route.ts
export async function GET(req:Request, { params }: { params: { hash: string } }) {
const response = await fetch("http://localhost:5000/api/image/${params.hash}")
const buff = Buffer.from(await response.data.arrayBuffer())
return new Response(buff, {headers: response.headers})
}
or try this
wait, are you using axios or fetch
axios
this is roughly what my data looks like now
it is pretty much the image data
However, when I set this as the URL for my image, it does not load the image.
Is it because it is a json object and not a readable stream of data?
could you show the code
look like you didn't set the header
this is my expressjs endpoint code
js res.status(200).header("Content-Type", "multipart/form-data").json(response.data)
i think I just need to add this ^
console.log(response)
no
the response is very large and contains a lot of other details apart from the image data
however, response.data is the image data
ok i guess this
res
.setHeader("Content-Type", response.headers["Content-Type"])
.setHeader("Content-length", response.headers["Content-Length"])
.send(response.data);
{"error":{"code":"ERR_HTTP_INVALID_HEADER_VALUE"}}
add a console.log(headers["Content-Type"])
console.log(response.headers['Content-Type'] ?
yes
do you know the image type?
png
Object [AxiosHeaders] {
date: 'Sun, 07 Jan 2024 15:31:09 GMT',
'content-type': 'application/json',
'content-length': '1804118',
connection: 'keep-alive',
'x-amzn-requestid': '70a4a3a0-db12-4fcf-b68d-5d0c2b4baa55',
'x-amz-apigw-id': 'RLOFpED9CYcEBOw=',
'x-amzn-trace-id': 'Root=1-659ac3bd-230f655021fbb37d7011bb2a'
oh there it is
I have kept it as allow all image types
res
.setHeader("Content-Type", response.headers["content-type"])
.setHeader("Content-Length", response.headers["content-length"])
.send(response.data);
it should show under New Post:?
yea show the code for it
export async function GET(req:Request, { params }: { params: { hash: string } }) {
return await fetch(`http://localhost:5000/api/image/${params.hash}`)
}
do you see the image with this url http://localhost:5000/api/image/${params.hash}?
change this
const response = await axios.get(AWS_URL + `/${req.params.imageID}`, { responseType: "blob" })
can you show me the url from AWS_URL?
that is sensitive to my project
After reading some responses on stackOverflow, I have a feeling that maybe I did not set up the API gateway correctly
yes the content-type is json
you need to set it to image
should it be base64?
but here is showing application/json
I don't know how you fetch the file but it should have the content-type
Object [AxiosHeaders] {
date: 'Sun, 07 Jan 2024 15:50:45 GMT',
'content-type': 'image/*',
'content-length': '1804118',
connection: 'keep-alive',
'x-amzn-requestid': '3c9f6253-15b5-4b65-9b28-935009923a09',
'x-amz-apigw-id': 'RLQ9aHjOCYcEsWA=',
'x-amzn-trace-id': 'Root=1-659ac855-37470a8f059fe9b101b28bc1'
}
lol
When I refresh /api/image/:hash it downloads the image
ok try it on your site
I am a little confused right now
I mean try here
It still does not work
check the error on dev console
params is not defined
I changed it up a little
router.get('/:imageID', async (req,res) =>{
res.redirect( AWS_URL + `/${req.params.imageID}`)
// try{
// const response = await axios.get(
// AWS_URL + `/${req.params.imageID}`,
// { responseType: "blob" }
// )
// // console.log(response)
// console.log(response.headers)
// res
// .status(200)
// .setHeader("Content-Type", response.headers["content-type"])
// .setHeader("Content-length", response.headers["content-length"])
// .send(response.data);
// }catch(error){
// res.status(400).json({error:error})
// }
})```
how do you render the image on the page?
try this on any page
<img src="/api/images/hash" />
yes it should work
try this

this is what I am doing
pretty much
I got this from the common mark docs
try this first
<img src="/api/images/hash" height={300} width={480} />
how about this?
still nothing
just this?
ye
what is the code on express now?
this.
doing redirect?
yea
It pretty much does the same thing that we were doing anyways
I just went back to our code and retried it! it does the exact same thing
one minute
lmao api/image/acfc7a71128cd0e8d436d64fa661bd506f7eb73e677f6374d729c955cb42f3db returns this
not work?
is this the actual image?
no
it isn;t
oh I realised what the error is
I am having an error when uploading the actual image
lol
i did a test by manually uploading an image and it worked
however, this is my code to upload images
router.post("/", uploadImages.single("image"), async (req, res) =>{
// Put an object into an Amazon S3 bucket.
const image = req.file.buffer;
const hash = crypto.createHmac('sha256', image)
.digest('hex');
console.log("imagehash:",hash);
console.log('awsurl:', AWS_URL + `/${hash}`)
try{
const response = await axios.put(
AWS_URL + `/${hash}`, // url
req.file.buffer, //file body
{
headers: {'content-type': "image/"},
},
)
console.log("response", response)
res.status(200).json({imageID: `${hash}`})
}catch(error){
console.log(error);
res.status(400).json({error: error})
}
})
i have to change the headers correct?
do you think this will work?
I think you need to provide the correct content-type
router.post("/", uploadImages.single("image"), async (req, res) =>{
// Put an object into an Amazon S3 bucket.
const image = req.file.buffer;
const fileExtension = req.file.originalname.split('.').pop();
const hash = crypto.createHmac('sha256', image)
.digest('hex');
console.log("imagehash:",hash);
console.log('awsurl:', AWS_URL + `/${hash}`)
try{
const response = await axios.put(
AWS_URL + `/${hash}`, // url
req.file.buffer, //file body
{
headers: {'content-type': `image/${fileExtension}`},
},
)
console.log("response", response)
res.status(200).json({imageID: `${hash}`})
}catch(error){
console.log(error);
res.status(400).json({error: error})
}
})```
I added the file extension! hopefully this works
that still didn't work
router.post("/", uploadImages.single("image"), async (req, res) =>{
// Put an object into an Amazon S3 bucket.
const image = req.file.buffer;
const fileExtension = req.file.originalname.split('.').pop();
const hash = crypto.createHmac('sha256', image)
.digest('hex');
console.log("imagehash:",hash);
console.log('awsurl:', AWS_URL + `/${hash}`)
try{
const response = await axios.put(
AWS_URL + `/${hash}`, // url
req.file, //file body
{
headers: {'content-type': `image/${fileExtension}`},
},
)
console.log("response", response)
res.status(200).json({imageID: `${hash}`})
}catch(error){
console.log(error);
res.status(400).json({error: error})
}
})
req.file instead of req.file.buffer
did not wokr
any error
good luck