我找不到一个不使用自定义express服务器的SvelteKit教程。有没有其他带有API端点的替代方案?
我已经尝试设置API端点,但是当数据发生更改时,我无法使其发送更新。
我找不到一个不使用自定义express服务器的SvelteKit教程。有没有其他带有API端点的替代方案?
我已经尝试设置API端点,但是当数据发生更改时,我无法使其发送更新。
+server.js
文件中,实现以下代码:// SSE only supports GET request
export async function GET({ url }) {
const stream = new ReadableStream({
start(controller) {
// You can enqueue multiple data asynchronously here.
const myData = ["abc", "def"]
myData.forEach(data => {
controller.enqueue(`data: ${data}\n\n`)
})
controller.close()
},
cancel() {
// cancel your resources here
}
});
return new Response(stream, {
headers: {
// Denotes the response as SSE
'Content-Type': 'text/event-stream',
// Optional. Request the GET request not to be cached.
'Cache-Control': 'no-cache',
}
})
}