Back

Trying to send html to client from server using appwrite function not working...

  • 0
  • React Native
  • Functions
  • Cloud
Musti
19 Aug, 2024, 17:18

Hi, my problem is that this code works:

TypeScript
import { Client, Account } from 'node-appwrite';


const client = new Client()
    .setEndpoint('https://cloud.appwrite.io/v1') 
    .setProject(PROJECT_ID)

const account = new Account(client);


export default async ({ req, res, log, error }) => {

    // get the query parameters, userId and secret from the url 
    const { userId, secret } = req.query

    const result = await account.updateVerification(userId, secret)

    // Respond with success or failure
    if (result.userId === userId) {
        // if the verification is successful
        return res.send('Verification successful')

    } else {
        // if the verification fails
        return res.send('Verification failed', 400)
    }

}

but if I want to respond with styled html by replacing the if condition content with the following:

TypeScript
if (result.userId === userId) {
            // If the verification successful, send HTML
            const htmlContent = `
                <!DOCTYPE html>
                <html lang="en">
                <head>
                    <meta charset="UTF-8">
                    <meta name="viewport" content="width=device-width, initial-scale=1.0">
                    <title>Verification Success</title>
                   
                </head>
                <body>
                    <div class="container">
                        <h1>Verification Successful!</h1>
                        <p>Your account has been successfully verified.</p>
                    </div>
                </body>
                </html>
            `;
            res.setHeader('Content-Type', 'text/html');
            return res.send(htmlContent);

I don't get the html, and instead just get:

This page isn’t working ********************.appwrite.global is currently unable to handle this request. HTTP ERROR 500

Note, the verification does still work, its just the webpage that isn't displaying.

Thanks in advance!

TL;DR
The developers are trying to send HTML from server to client using an Appwrite function but facing issues. The solution involves setting headers and using the correct format for sending the HTML content in the response. Instead of just setting the header, they should use `res.send(htmlContent, 200, { "content-type": "text/html" })`.
ideclon
19 Aug, 2024, 17:26

It should be

TypeScript
return res.send(htmlContent, 200, {
  "content-type", "text/html"
}
ideclon
19 Aug, 2024, 17:26

Appwrite doesn't set headers like that

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more