3

How to return non-HTML responses from Astro | Steve Fenton

 1 year ago
source link: https://www.stevefenton.co.uk/2022/09/how-to-return-non-html-responses-from-astro/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to return non-HTML responses from Astro

There are some cases in Astro where you need to return a non-HTML response. For example, and XML or JSON file.

To do this, create a file with the appropriate name, but add .js on the end. For example, if you want search.json place the file in the pages directory with the name search.json.js.

Inside the file, you need to export a getter that returns a response with a body, for example:

export const get = () => {
    return {
        body: '[1, 2, 3, 4]'
    };
};

When you save this file and navigate to /search.json in your browser, you’ll see the JSON response, [1, 2, 3, 4].


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK