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

196
Views
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 answers
Answer question

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