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

123
Views
Unexpected file transfer behaviour when building API using Express

Here is how my API works:

How my API works

You can find SeaweedFS here on GitHub.
And the code here:

// /drivers/seaweedfs.js Defines how API interacts with SeaweedFS
const { error } = require("console");
const http = require("http");
module.exports = class Weed {
  constructor(mserver) {
    this.mserver = new URL(mserver);
  }

  get(fileId) {
    return new Promise((resolve, reject) => {
      let options = {
        hostname: this.mserver.hostname,
        port: this.mserver.port,
        method: "GET",
        path: `/${fileId}`,
        timeout: 6000,
      };
      let data;
      const fileReq = http.request(options, (res) => {
        console.log(`Statuscode ${res.statusCode}`);
        res.on("data", (response) => {
          data += response;
        });
        res.on("end", () => {
          resolve(data);
        });
      });
      fileReq.on("error", () => {
        console.error(error);
        reject();
      });
      fileReq.end();
    });
  }
};

// /routes/file.js An Express router
const express = require("express");
const router = express.Router();
const Weed = require("../drivers/seaweedfs");
let weedClient = new Weed("http://localhost:60002");

router.get("/:fileId", (req, res) => {
    weedClient.get(req.params.fileId)
    .then(data=>{
        res.write(data)
        res.end()
    })
}
)

module.exports = router;

MongoDB driver not yet implemented.

When I try to GET a file(using Firefox, Hoppscotch says Could not send request: Unable to reach the API endpoint. Check your network connection and try again.), I get something whose MIME type is application/octet-stream for some reason. It's bigger than the original file. I know there must be some problems with my code, but I don't know where and how to fix it.

about 4 years ago · Juan Pablo Isaza
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!