I am using the following code to first download a file, and then send a response to the client:
async function download(uri, filename, callback) {
request.head(uri, async function (err, res, body) {
console.log("content-type:", res.headers["content-type"]);
console.log("content-length:", res.headers["content-length"]);
request(uri).pipe(fs.createWriteStream(filename)).on("close", callback);
});
}
async function ytsSearch(name) {
const movies = await yts.listMovies({ limit: 3, query_term: name });
return movies;
}
async function searchAndDownload(name) {
movies = await ytsSearch(name);
movies.movies.forEach(async function (movie) {
if (
fs.existsSync(
path.join(__dirname, "covers", movie.title_english + ".png")
)
) {
return;
} else {
await download(
movie.medium_cover_image,
`covers/${movie.title_english}.png`,
function () {
console.log("done");
}
);
}
});
return movies;
}
app.use("/movie", function (req, res) {
const { name } = req.body;
searchAndDownload(name).then((movies) => console.log(movies));
});
However, On the client side, The response is received first, before the file is saved on the server. I know I am probably doing something wrong with so many asynchronous functions, but can't seem to find out what exactly is going wrong.
As you can see, the response is sent first, and the picture is downloaded second.
Minimal Reproducible Code:
const express = require("express");
const app = express();
const http = require("http");
const request = require("request");
const server = http.createServer(app);
const cors = require("cors");
const yts = require("yts");
const fs = require("fs");
const path = require("path");
const port =5000
async function download(uri, filename, callback) {
request.head(uri, async function (err, res, body) {
console.log("content-type:", res.headers["content-type"]);
console.log("content-length:", res.headers["content-length"]);
request(uri).pipe(fs.createWriteStream(filename)).on("close", callback);
});
}
async function ytsSearch(name) {
const movies = await yts.listMovies({ limit: 3, query_term: name });
return movies;
}
async function searchAndDownload() {
movies = await ytsSearch("minions");
movies.movies.forEach(async function (movie) {
if (
fs.existsSync(
path.join(__dirname, "covers", movie.title_english + ".png")
)
) {
return;
} else {
await download(
movie.medium_cover_image,
`covers/${movie.title_english}.png`,
function () {
console.log("done");
}
);
}
});
return movies;
}
server.listen(port, () => {
searchAndDownload().then((movies)=>console.log(movies))
});
package.json
{
"name": "torparty",
"version": "1.0.0",
"description": "Group Torrent Streaming",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/diivi/torparty.git"
},
"author": "Divyansh Singh",
"license": "ISC",
"bugs": {
"url": "https://github.com/diivi/torparty/issues"
},
"homepage": "https://github.com/diivi/torparty#readme",
"dependencies": {
"express": "^4.17.1",
"request": "^2.88.2",
"socket.io": "^4.1.3",
"socket.io-client": "^4.1.3",
"yts": "^2.0.0"
}
}