我在路线文件夹中使用了共享的getapi.json.js api“ getter”。绝对可以,但是我想知道是否通过不为每个唯一的路由和/或组件创建单独的api文件而丢失了一些SSR缓存/优化。我通过执行此共享文件是否犯了错误?
这是我共享的getapi.json.js文件...
import fetch from 'node-fetch'
export const get = async (req, res) => {
const coll = req.query.coll;
const data = await fetch(`http://local.yardsale.com:6098/api/${coll}`, {
credentials: 'include'
});
const contents = await data.text();
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.end(contents);
}
编辑:从路由中添加呼叫代码以供将来搜索...
<script context="module">
export async function preload(page, session) {
return this.fetch(`getapi.json?coll=user`)
.then(r => r.json())
.then(users => {
return { users };
});
}
</script>