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

203
Visualizações
How to implement download file from heroku server in nodejs

I have a nodejs application where I have created project structure. package.json and index.js files, one json file also which I want that user can download it.

This is how my project structure

enter image description here

I have deployed nodejs application on heroku server. It is running perfectly, able to access index.js

How I can implement a functionality here so users can download swagger.json also. I tried to access this like https://heroku-address/swagger.json but it is showing only "Hello World"

index.js

var http = require("http");

http.createServer(function (request, response) {

   // Send the HTTP header 
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});

   // Send the response body as "Hello World"
   response.end('Hello World\n');
}).listen(process.env.PORT);
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Let's start with defining server and app variables,

var express = require('express')
var app = module.exports = express();

Now,To show a tags on which if users click file will be downloaded,below is code to show links,

app.get('/', function(req, res){
   res.send('<ul>'
    + '<li> <a href="/package.json">package.json</a>.</li>'
    + '<li> <a href="/swagger.json">swagger.json</a>.</li>'
    + '</ul>');
});

Now,When user clicks on one of the above link then code shown below will be executed,and file will be downloaded,

app.get('/:file(*)', function(req, res, next){
    var file = req.params.file
    , path = __dirname + '/' + file;

    res.download(path);
});

finally code for listening on port,

app.listen(8080);
console.log('Express started on port %d', 8080);

So,your full server.js file will look like,

var express = require('express')
  , app = module.exports = express();
 
app.get('/', function(req, res){
  res.send('<ul>'
    + '<li>Download <a href="/package.json">package.json</a>.</li>'
    + '<li>Download <a href="/swagger.json">swagger.json</a>.</li>'
    + '</ul>');
});

app.get('/:file(*)', function(req, res, next){
  var file = req.params.file
    , path = __dirname + '/' + file;

  res.download(path);
});

app.listen(8080);
console.log('Express started on port %d', 8080);

over 4 years ago · Santiago Trujillo 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