For my latest SvelteKit project, I was developing a Joke API using Bun as the runtime and TypeScript as the programming language. The goal of this API was to fetch one random dad joke from a text file that contained 363 jokes. Everything was going smoothly until I encountered a problem - the file reader couldn't locate the file. Even though I provided the correct file path ./jokes.txt
, the reader kept throwing errors. I initially suspected that Bun's integration might be causing the issue due to its newness, so I attempted to use readFileSync
instead, but that didn't resolve the problem either.
Here is the snippet of my code:
import { readFileSync } from "fs";
import { db } from "$lib/db";
import { sql } from "drizzle-orm";
import { sqliteTable, integer, text } from 'drizzle-orm/sqlite-core';
export async function GET() {
let file = readFileSync("./jokes.txt", "utf-8");
const jokes = file.split("\n");
// More code here...
return {
joke: joke
}
}
Below is an overview of my file structure:
.sveltekit
node_modules
src
lib
db.ts
routes
api
+server.ts
jokes.txt
jokes-backup.txt
+layout.svelte
+page.svelte
app.d.ts
app.html
app.pcss
static
.gitignore
.npmrc
bun.lockb
package.json
postcss.config.cjs
README.md
svelte.config.js
tailwind.config.cjs
tsconfig.json
vite.config.ts