I'm trying to use sveltekit with a custom express server as outlined in the sveltekit doc here
Here is my server :
import { handler } from '../build/handler.js'
import express from 'express';
const app = express();
app.use((req, res, next) => {
const corsWhitelist = [
"http://localhost:5000", // front section
"http://localhost:4000", //menumanagement section
"http://localhost:2000", //owner section
"http://localhost:2500", // orders dashboard section
"http://localhost:5173", //sveltekit apps
];
if (corsWhitelist.indexOf(req.headers.origin) !== -1) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header("Access-Control-Allow-Headers", "Origin, X-PINGOTHER , X-Requested-With, Content-Type, Accept");
res.header({'Access-Control-Allow-Credentials': true});
res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE,OPTIONS');
}
next();
}); //end app use for muliple origins
// add a route that lives separately from the SvelteKit app
app.get('/healthcheck', (req, res) => {
console.log("healthcheck inside expxress server")
res.end('healthcheckok');
});
// let SvelteKit handle everything else, including serving prerendered pages and static assets
app.use(handler);
app.get('/expresskit', (req, res)=>{
console.log("expresskit handle ....")
res.end("expresskit")
})
app.listen(3000, () => {
console.log('custom server is listening on port 3000');
});
created a sveltekit and in the index page - in the +page.svelte I created a button
<button on:click={getexpresskit}>get expresskit</button>
in the script I run the function :
async function getexpresskit(){
let response = await fetch('http://localhost:3000/expresskit')
let serverdata =await response.json()
console.log("-------------", serverdata)
} //end getexpresskit
I get this in the console : I get 404 /expresskit not found
Error: Not found: /expresskit
at resolve (file:///D:/mycode/sveltekit/joyofcodesocket/socketjoyofcode/build/server/index.js:3915:18)
at resolve (file:///D:/mycode/sveltekit/joyofcodesocket/socketjoyofcode/build/server/index.js:3749:34)
at #options.hooks.handle (file:///D:/mycode/sveltekit/joyofcodesocket/socketjoyofcode/build/server/index.js:3981:61)
at respond (file:///D:/mycode/sveltekit/joyofcodesocket/socketjoyofcode/build/server/index.js:3747:43)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
I tried to rebuild the kit folder again, so I ran npm run build in my kit folder, get the following error :
node:internal/event_target:1012
process.nextTick(() => { throw err; });
^
Error: 405 /
To suppress or handle this error, implement `handleHttpError` in https://kit.svelte.dev/docs/configuration#prerender
at file:///D:/mycode/sveltekit/joyofcodesocket/socketjoyofcode/node_modules/@sveltejs/kit/src/core/config/options.js:212:13
at file:///D:/mycode/sveltekit/joyofcodesocket/socketjoyofcode/node_modules/@sveltejs/kit/src/core/postbuild/prerender.js:64:25
at save (file:///D:/mycode/sveltekit/joyofcodesocket/socketjoyofcode/node_modules/@sveltejs/kit/src/core/postbuild/prerender.js:403:4)
at visit (file:///D:/mycode/sveltekit/joyofcodesocket/socketjoyofcode/node_modules/@sveltejs/kit/src/core/postbuild/prerender.js:236:3)
Emitted 'error' event on Worker instance at:
at [kOnErrorMessage] (node:internal/worker:300:10) at [kOnMessage] (node:internal/worker:311:37)
at MessagePort.<anonymous> (node:internal/worker:212:57)
at [nodejs.internal.kHybridDispatch] (node:internal/event_target:737:20)
at exports.emitMessage (node:internal/per_context/messageport:23:28)
Node.js v18.16.1
I'm aware that the error pointing to build/index.js instead of my server but I don't know how to fix it. Where do I correct the build script to point to my custom express server? How do I fix it?
Is there a resource with a detailed step by step on how to create and use custom express server instead of asking the same question here>?