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

199
Vistas
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 Respuestas
Responde la pregunta

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