我正在尝试建立SvelteKit
项目并将其托管在服务器上nginx
, using @sveltejs/adapter-static
。该应用程序在本地开发时工作正常,但在构建并上传时我得到了500 JSON.parse: unexpected character at line 1 column 1 of the JSON data
对于某些路线。
有问题的路线的数据来源是json
文件(项目的一部分)。
我的文件夹结构是:
src/
├── data/
│ └── my_data.json
└── routes/
├── api/
│ └── something.json.js
└── area/
└── [method].svelte
something.json.js
import * as siteData from "../data/my_data.json";
export async function GET({ url }) {
const param = url.searchParams.get("method");
const body = siteData[param];
return {
body
}
})
[method].svelte
<script context="module">
export const load = async ({ params, fetch }) => {
const response = await fetch(`/api/something.json?method=${params.method}`);
const posts = await response.json();
return {
props: {
posts
}
};
};
</script>
<script>
export let posts;
</script>
<div>
<!-- do something with "data" here -->
</div>
svelte.config.js
import adapter from '@sveltejs/adapter-static';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter({
pages: 'build',
assets: 'build',
fallback: 'index.html',
precompress: false
})
}
};
export default config;
nginx.conf
http {
server {
listen 80;
include /etc/nginx/mime.types;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/index.html $uri.html /index.html;
}
}
}
通过此设置:
我的(疯狂的)猜测:
- 这永远不会起作用,因为没有实际的后端来处理端点代码
-
nginx
尝试呼叫/服务时配置错误/api/something.json