API Routes
Analog supports defining API routes that can be used to serve data to the application.
Defining an API Route
API routes are defined in the src/server/routes
folder. API routes are also filesystem based,
and are exposed under the default /api
prefix in development.
import { defineEventHandler } from 'h3';
export default defineEventHandler(() => ({ message: 'Hello World' }));
Defining XML Content
To create an RSS feed for your site, set the content-type
to be text/xml
and Analog serves up the correct content type for the route.
//server/routes/rss.xml.ts
import { defineEventHandler, setHeader } from 'h3';
export default defineEventHandler((event) => {
const feedString = `<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
</rss>
`;
setHeader(event, 'content-type', 'text/xml');
return feedString;
});
Note: For SSG content, set Analog to prerender an API route to make it available as prerendered content:
// vite.config.ts
...
prerender: {
routes: async () => {
return [
...
'/api/rss.xml',
...
.
];
},
sitemap: {
host: 'https://analog-blog.netlify.app',
},
},