Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
Downloading File From Client Side Node.js

so I am trying to build a website that allows users to download files that are located in the server computer when the users access the website and click a download button.

I wish to use as few libraries as possible due to some real world limitations. Ideally no Express or Ajax. And I think it should be fully possible with just vanilla node.js

From my search on the internet it seems most of the code is of this form:

const fs = require('fs');
const https = require('https');
  
// URL of the image
const url = 'GFG.jpeg';
  
https.get(url,(res) => {
    // Image will be stored at this path
    const path = `${__dirname}/files/img.jpeg`; 
    const filePath = fs.createWriteStream(path);
    res.pipe(filePath);
    filePath.on('finish',() => {
        filePath.close();
        console.log('Download Completed'); 
    })
})

However, the code doesn't seem to be doing what I want. First, it requires an url, so it is more about directing a resource online to another location. Whereas I want to actually serve a locally stored file on the server to users when they access the website.

Second, it appears to be downloading to the server computer. But what I want is to let users download to their own client devices. Basically the normal download function you would encounter when you want to download something on the Internet and you see your browser's "Download" section having some new entries.

How can I achieve what I want?

I'm a total noob at this, so it would be great if I can get a skeleton code with some dummy file or pathname.

Appreciate any guidance. Thanks!

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You are missing an http.server. http.get just does a web request and as you said you don't want to do that.

Here is some example code creating a server and serving a single file without using express:

const fs = require('fs');
const path = require('path');
const http = require('http');

http.createServer(function(request, response) {
    var filePath = path.join(__dirname, '/files/img.jpeg');

    response.writeHead(200);

    var readStream = fs.createReadStream(filePath);
    readStream.pipe(response);
}).listen(2000);
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda