如果不使用自定义快递服务器,我找不到 SvelteKit 的教程。 API端点是否有任何替代方案?
我试过自己设置 API 端点,但我无法让它在数据更改时发送更新。
感谢@Paolo 的想法。这是一个更具体的例子:
在您的
+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',
}
})
}