I am trying to read a file that is local to my computer into a blob using the node-fetch library, but each time I do, I get this Invalid Url Error. My code is the following:
const express = require('express');
const cors = require('cors');
const bodyParser = require("body-parser");
const fetch = (...args) =>
import('node-fetch').then(({ default: fetch }) => fetch(...args));
const blobFromSync = (...args) =>
import('node-fetch').then(({ blobFromSync }) => fetch(...args));
const blobFrom = (...args) =>
import('node-fetch').then(({ blobFrom }) => fetch(...args));
const fileFrom = (...args) =>
import('node-fetch').then(({ fileFrom }) => fetch(...args));
const app = express();
const port = 3000;
//Here we are configuring express to use body-parser as middle-ware.
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*")
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
next()
});
app.get('/recorded', async (req, res) => {
try {
let file_location = './path/to/video.mp4';
const file = fileFrom(file_location);
const chunkSize = 40000
for (let start = 0; start < file.size; start = chunkSize) {
const chunk = file.slice(start, start chunkSize 1)
//Will eventuay doa fetch
//await fetch(url, { method: 'post', body: chunk, headers: headers, }).then(res => res.text())
}
res.status(200);
res.send().end();
} catch (e) {
console.log(e);
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
});
The error is the following (its also uncatchable):
TypeError [ERR_INVALID_URL]: Invalid URL
at new NodeError (node:internal/errors:377:5)
at URL.onParseError (node:internal/url:563:9)
at new URL (node:internal/url:643:5)
at new Request (file:///Users/xxxxxx/Development/terminal-backend/node_modules/node-fetch/src/request.js:55:16)
at file:///Users/xxxx/Development/terminal-backend/node_modules/node-fetch/src/index.js:51:19
at new Promise (<anonymous>)
at fetch (file:///Users/xxxx/Development/terminal-backend/node_modules/node-fetch/src/index.js:49:9)
at /Users/xxxxx/Development/terminal-backend/src/server.js:11:53 {
input: './path/to/video.mp4',
code: 'ERR_INVALID_URL'
}
blobFromSync and blobFrom also throw the same error. If this is a local file, why am I get invalid url?
CodePudding user response:
The main issue is with the how you're importing the functions other than fetch
i.e. const fileFrom = (...args) => import('node-fetch').then(({ fileFrom }) => fetch(...args));
Imports fileFrom
but uses fetch
Simplest fix is as follows
const fetch = (...args) =>
import('node-fetch').then(({ default: fetch }) => fetch(...args));
const blobFromSync = (...args) =>
import('node-fetch').then(({ blobFromSync }) => blobFromSync(...args));
const blobFrom = (...args) =>
import('node-fetch').then(({ blobFrom }) => blobFrom(...args));
const fileFrom = (...args) =>
import('node-fetch').then(({ fileFrom }) => fileFrom(...args));