我正在尝试将load
sveltekit 中的函数与 ssr 一起使用。
由于某种原因,我连接被拒绝:
<script context="module">
import Jobs from '$api/jobs';
export async function load({ fetch }) {
const jobs = await new Jobs(fetch).getAll();
console.log(jobs);
return {
props: {
jobs
}
};
}
</script>
Run Code Online (Sandbox Code Playgroud)
工作.js
import Api from './create-api';
class Jobs extends Api {
constructor(fetch) {
super(fetch);
}
async getAll() {
return await this.api('/jobs');
}
async getByTag(tag) {
return await this.api('/jobs/tags/' + tag);
}
async getByUsername(username) {
return await this.api('/jobs/users/' + username.toLowerCase());
}
async getAllMine() {
return await this.api('/jobs/me');
}
async create(job) {
return await this.api('/jobs', job, { method: 'POST' });
}
async update(job) {
return await this.api('/jobs/' + job.id, job, { method: 'PUT' });
}
async deleteById(id) {
return await this.api('/jobs/' + id, {}, { method: 'DELETE' });
}
async getById(id) {
console.log(id);
return await this.api('/jobs/' + id, {}, { method: 'GET' });
}
}
export default Jobs;
Run Code Online (Sandbox Code Playgroud)
创建-api.js
import { browser } from '$app/env';
class Api {
constructor(fetch) {
this.fetch = fetch;
}
async api(path, body = {}, opts = {}) {
path = import.meta.env.VITE_API_ENDPOINT + path;
body = JSON.stringify(body);
const method = opts.method || 'GET';
const headers = {};
if (browser) {
const token = localStorage.getItem('token');
headers.Authorization = token ? 'Bearer ' + token : '';
}
const res = await this.fetch(path, {
method: opts.method || 'GET',
body: method === 'GET' ? null : body,
headers
});
if (res.ok) {
return await (opts.raw ? res.text() : res.json());
}
throw res;
}
}
export default Api;
Run Code Online (Sandbox Code Playgroud)
这是错误:
7:54:21 PM [vite] page reload src/routes/jobs/index.svelte
request to http://localhost:3001/api/1/jobs failed, reason: connect ECONNREFUSED ::1:3001
FetchError: request to http://localhost:3001/api/1/jobs failed, reason: connect ECONNREFUSED ::1:3001
at ClientRequest.<anonymous> (file:///home/ettinger/www/grazily.com/grazily-ui/node_modules/@sveltejs/kit/dist/install-fetch.js:5779:11)
at ClientRequest.emit (node:events:526:28)
at ClientRequest.emit (node:domain:475:12)
at Socket.socketErrorListener (node:_http_client:442:9)
at Socket.emit (node:events:526:28)
at Socket.emit (node:domain:475:12)
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Run Code Online (Sandbox Code Playgroud)
api 工作正常,onMount
只是不在加载函数中。
归档时间: |
|
查看次数: |
1520 次 |
最近记录: |