Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

659
Views
Punto final de SvelteKit: conversión desde Node/Express

Nuevo en SvelteKit y trabajando para adaptar un punto final de un servidor Node/Express para hacerlo más genérico para poder aprovechar los adaptadores SvelteKit. El punto final descarga archivos almacenados en una base de datos a través de node-postgresql.

Mi punto final funcional en Node/Express se ve así:

 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) }

Esto es lo que tengo para [filenum].json.ts en SvelteKit hasta ahora...

 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 } } }

¿Cuál es la forma correcta de hacer esto con SvelteKit sin crear una dependencia en Node? Según los documentos de punto final de SvelteKit ,

No interactuamos con los objetos req/res con los que podría estar familiarizado del módulo http de Node o marcos como Express, porque solo están disponibles en ciertas plataformas. En su lugar, SvelteKit traduce el objeto devuelto en lo que requiera la plataforma en la que está implementando su aplicación.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

ACTUALIZACIÓN: El error se corrigió en SvelteKit. Este es el código actualizado que funciona:

 // 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 } }

y

 // 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!