0

I might be close, but not quite there yet as the below throws Error: Invalid response from route /api: handler should return a Response object, probably I do return just an object and not a Response?

routes/api/+server.ts

/** @type {import('./$types').RequestHandler} */
export async function GET({ url }) { 

...

 var server = http.createServer(function (req, res) {
     pool.connect(async (err, client, done) => {
        if (err) throw err;
        const query = new QueryStream(querystring)
        const stream = client.query(query);
        stream.on('end', done)
        return stream.pipe(JSONStream.stringify()).pipe(res)
     })
  })

  return server;

}

src/lib/api.js

async function getDatabaseData (/** @type {string} */ url) {
    // fetch internal server api.
    const response = await fetch(url, {
        method: 'GET',
    });
    let d = await response.json();
    return d.message;
}

I assume returning 'server' is all wrong, but this is just my another attempt. Appreciate an heads up, thank you

Edit: with the code in the post below, I am now getting data correctly on the server side, but still facing the issue of streaming it to the client via a 'stringified' Response. How do I measure progress while streaming a postgres database query via pg-query-stream?

2
  • Do you need the http.createServer wrapper? I'm able to get your example working in mine without it, but I'm using the node-adapter with SvelteKit. Commented Jun 28, 2024 at 15:56
  • actually no, I don't need that wrapper. I am also using the node adapter. Appreciate your advice Commented Jun 28, 2024 at 16:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.