Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

655
Vistas
SvelteKit endpoint: converting from Node/Express

New to SvelteKit and working to adapt an endpoint from a Node/Express server to make it more generic so as to be able to take advantage of SvelteKit adapters. The endpoint downloads files stored in a database via node-postgresql.

My functional endpoint in Node/Express looks like this:

import stream from 'stream'
import db from '../utils/db'

export async function download(req, res) {
  const _id = req.params.id
  const sql = "SELECT _id, name, type, data FROM files WHERE _id = $1;"
  const { rows } = await db.query(sql, [_id])
  const file = rows[0]
  const fileContents = Buffer.from(file.data, 'base64')
  const readStream = new stream.PassThrough()
  readStream.end(fileContents)
  res.set('Content-disposition', `attachment; filename=${file.name}`)
  res.set('Content-Type', file.type)
  readStream.pipe(res)
}

Here's what I have for [filenum].json.ts in SvelteKit so far...

import stream from 'stream'
import db from '$lib/db'

export async function get({ params }): Promise<any> {
  const { filenum } = params
  const { rows } = await db.query('SELECT _id, name, type, data FROM files WHERE _id = $1;', [filenum])
  
  if (rows) {
    const file = rows[0]
    const fileContents = Buffer.from(file.data, 'base64')
    const readStream = new stream.PassThrough()
    readStream.end(fileContents)
    let body
    readStream.pipe(body)

    return {
      headers: {
        'Content-disposition': `attachment; filename=${file.name}`,
        'Content-type': file.type
      },
      body
    }
  }
}

What is the correct way to do this with SvelteKit without creating a dependency on Node? Per SvelteKit's Endpoint docs,

We don't interact with the req/res objects you might be familiar with from Node's http module or frameworks like Express, because they're only available on certain platforms. Instead, SvelteKit translates the returned object into whatever's required by the platform you're deploying your app to.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

UPDATE: The bug was fixed in SvelteKit. This is the updated code that works:

// src/routes/api/file/_file.controller.ts
import { query } from '../_db'

type GetFileResponse = (fileNumber: string) => Promise<{
  headers: {
      'Content-Disposition': string
      'Content-Type': string
  }
  body: Uint8Array
  status?: number
} | {
  status: number
  headers?: undefined
  body?: undefined
}>

export const getFile: GetFileResponse = async (fileNumber: string) => {
  const { rows } = await query(`SELECT _id, name, type, data FROM files WHERE _id = $1;`, [fileNumber])
  if (rows) {
    const file = rows[0]
    return {
      headers: {
        'Content-Disposition': `attachment; filename="${file.name}"`,
        'Content-Type': file.type
      },
      body: new Uint8Array(file.data)
    }
  } else return {
    status: 404
  }
}

and

// src/routes/api/file/[filenum].ts
import type { RequestHandler } from '@sveltejs/kit'
import { getFile } from './_file.controller'

export const get: RequestHandler = async ({ params }) => {
  const { filenum } = params
  const fileResponse = await getFile(filenum)
  return fileResponse
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda