how to download and save video using nodejs - node.js

i want to download video from tiktok
using nodejs and tiktok-scraper-without-watermark package
but when the mp4 file downloaded wont open
is there is any more work
http://localhost:7780/tik?title=test&url=https://www.tiktok.com/#youneszarou/video/6942436555692805381
my attempt is
const https = require("https");
const fs = require("fs");
const express = require("express");
const router = express.Router();
const tiktok = require("tiktok-scraper-without-watermark");
const pool = require("../config/db");
router.get("/", async (req, res) => {
try {
const { title, url } = req.query;
const path = process.cwd() + `/videos/${title}.mp4`;
const tik = await tiktok.tiktokdownload(url);
//console.log("tik", tik);
const requ = https.get(tik.nowm, (response) => {
const file = fs.createWriteStream(path);
response.pipe(file);
file.on("error", function (err) {
console.log("err", err);
});
file.on("finish", function () {
file.close();
console.log("done");
});
});
requ.on("err", (error) => {
console.log("error", error);
});
res.status(200).json(tik);
} catch (error) {
console.log("error", error);
res.status(401).json(error);
}
});
module.exports = router;
the result of tiktok package is
{
nowm: "https://ttdownloader.com/dl.php?v=YTo0OntzOjk6IndhdGVybWFyayI7YjowO3M6NzoidmlkZW9JZCI7czozMjoiYzc5YWZmNmIwYzkwNzk2ZDMxODg0YmU0MmMxMjJjNGIiO3M6MzoidWlkIjtzOjMyOiJlZDBiNGY4ZWFmZjJjMjllNjZiMzEzZjc0YTMxZWYzOCI7czo0OiJ0aW1lIjtpOjE2NDg5MTAxMjg7fQ==",
wm: "https://ttdownloader.com/dl.php?v=YTo0OntzOjk6IndhdGVybWFyayI7YjoxO3M6NzoidmlkZW9JZCI7czozMjoiYzc5YWZmNmIwYzkwNzk2ZDMxODg0YmU0MmMxMjJjNGIiO3M6MzoidWlkIjtzOjMyOiJlZDBiNGY4ZWFmZjJjMjllNjZiMzEzZjc0YTMxZWYzOCI7czo0OiJ0aW1lIjtpOjE2NDg5MTAxMjg7fQ==",
audio: "https://ttdownloader.com/mp3.php?v=YTozOntzOjc6InZpZGVvSWQiO3M6MzI6ImM3OWFmZjZiMGM5MDc5NmQzMTg4NGJlNDJjMTIyYzRiIjtzOjM6InVpZCI7czozMjoiZWQwYjRmOGVhZmYyYzI5ZTY2YjMxM2Y3NGEzMWVmMzgiO3M6NDoidGltZSI7aToxNjQ4OTEwMTI4O30="
}

Related

POST http://localhost:3000/sort net::ERR_ABORTED 500 (Internal Server Error)

I am trying to upload a file to the API, sort the numbers and then return the result in another text file that is available to download. I upload the file, and when I start the calculation I get the Internal Server Error. The API is running on port 3000 and I start the React App.js on port 3001.
Is there something I'm doing wrong?
This is the API's app.js:
const express = require('express');
const multer = require('multer');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
app.use(bodyParser.text({ type: 'text/plain' }));
app.post('/sort', upload.single('inputFile'), (req, res) => {
console.log(req.file)
const input = req.file.buffer.toString().split('\n').map(Number);
const result = input.sort((a, b) => b - a);
const resultText = result.join('\n');
fs.writeFile('result.txt', resultText, (err) => {
if(err) throw err;
res.send('File succesfully sorted!');
});
res.set('Content-Type', 'text/plain');
res.send(resultText);
});
app.listen(3000, () => {
console.log('API is listening on port 3000');
});
This is the React App.js:
const [inputFile, setInputFile] = useState(null);
const [result, setResult] = useState(null);
const [processingTime, setProcessingTime] = useState(null);
const handleFileUpload = (event) => {
setInputFile(event.target.files[0]);
};
const startCalculation = async (event) => {
event.preventDefault();
const startTime = performance.now();
const formData = new FormData();
formData.append('inputFile', inputFile);
console.log(inputFile)
const response = await fetch("http://localhost:3000/sort", {
method: 'POST',
body: formData,
mode: 'no-cors',
});
const data = await response.text();
console.log(data);
setResult(data);
setProcessingTime(performance.now() - startTime);
};
const handleDownload = (event) => {
event.preventDefault();
const file = new Blob([result], {
type: 'text/plain'
});
const fileURL = URL.createObjectURL(file);
const link = document.createElement('a');
link.href = fileURL;
link.download = 'result.txt';
link.click();
};
The issue is on the client you are setting the input name to inputFile, however, on the backend you are telling Multer that the input name is myFile.
Change from this:
upload.single("myFile")
To this:
upload.single("inputFile")

How can I fix the error when using the Sharp library?

I want to resize and compress images using sharp library in node.js. The code was taken from this article, but for some reason returns an error.
Error
PS D:\Code\Web\image-compressor-nodejs> node index.js
[Error: ./uploads/2022-08-10T18:35:49.251Z-2021-11-18_23.44.45.png.webp: unable to open for write
windows error: ���������� �� �������� �������.
]
Code
const express = require("express");
const multer = require("multer");
const sharp = require("sharp");
const fs = require("fs");
const app = express();
const storage = multer.memoryStorage();
const upload = multer({ storage });
app.use(express.static("./uploads"));
app.post("/", upload.single("picture"), async (req, res) => {
try {
fs.access("./uploads", (error) => {
if (error) {
fs.mkdirSync("./uploads");
}
});
const { buffer, originalname } = req.file;
const timestamp = new Date().toISOString();
const ref = `${timestamp}-${originalname}.webp`;
await sharp(buffer)
.webp({ quality: 20 })
.toFile("./uploads/" + ref);
const link = `http://localhost:3000/${ref}`;
return res.json({ link });
} catch (error) {
console.log(error);
}
});
app.listen(3000);

postman when testing GET it returns 200 but with an empty body

When I m trying to test my GET API using postman it returns 200 but with an empty body, The data I'm expecting to get do not show up.
Find my server.js file and the screenshot of POSTMAN result
app.get('/api/articles/:name', async (req, res) => {
try {
const articleName = req.params.name;
const client = await MongoClient.connect('mongodb://localhost:27017', { useNewUrlParser: true });
const db = client.db('my-blog');
const articleInfo = await db.collection('articles').findOne({ name: articleName })
res.status(200).json(articleInfo)
client.close()
}
catch (error) {
res.status(500).json({ message: 'error connecting to db', error })
}
})
here i have updated your code as below and please move your server.js outside of /src folder. its working now.
const express = require('express')
const bodyParser = require('body-parser')
const {MongoClient} = require("mongodb");
const url = 'mongodb://127.0.0.1:27017';
const app = express();
app.use(bodyParser.json());
app.get('/api/articles/:name', async (req, res) => {
try {
const articleName = req.params.name;
MongoClient.connect(url, async (err, db) => {
const client = db.db('article');
const articleInfo = await client.collection('articles').findOne({title: articleName})
res.send(articleInfo)
});
} catch (error) {
res.status(500).json({ message: 'Error connecting to db', error });
}
});
app.listen(8000, () => console.log('Listening on port 8000'));

Storing Images in Mongodb With GridFS Using Express Server

I am trying to upload, save and display images in MongoDB using express server and GridFS.
Data is sent with FormData with vue.js on the front-end and that part works well.
Images are getting to the server, and it stores that data, but I can not display the uploaded image, instead I get this
It displays the Image in some encoding I guess.
Here is server code for receiving, storing and displaying images.
const express = require('express')
const mongodb = require('mongodb')
const path = require('path')
const crypto = require('crypto')
const mongoose = require('mongoose')
const multer = require('multer')
const GridFsStorage = require('multer-gridfs-storage')
const Grid = require('gridfs-stream')
const methodOverride = require('method-override')
const config = require('../../config/keys')
const morgan = require('morgan')
const images = express()
// MIDDLEWARE
images.use(methodOverride('_method'))
// images.use(morgan("default"))
// MONGO URI
const mongoURI = config.dbUrl
// CREATE MONGO CONNECTION
const conn = mongoose.createConnection(mongoURI)
// INITALIZE GRID FS
let gfs
conn.once('open', () => {
gfs = Grid(conn.db, mongoose.mongo)
gfs.collection('uploads')
})
// CREATING STORAGE ENGINE
const storage = new GridFsStorage({
url: mongoURI,
file: (req, file) => {
return new Promise((resolve, reject) => {
const filename = file.originalname
const fileInfo = {
filename,
bucketName: 'uploads'
}
resolve(fileInfo)
})
}
})
const upload = multer({ storage })
// POST REQUEST UPLOADS FILE TO DATABASE
images.post('/', upload.single('image'), (req, res) => {
console.log('POST REQUEST TO HTTP://LOCALHOST:3000/API/BACKEND/IMAGES/')
res.json(req.file)
console.log(req.file)
})
// GET REQUEST
images.get('/', async (req, res) => {
console.log('GET REQUEST TO HTTP://LOCALHOST:3000/API/BACKEND/IMAGES/')
const posts = await load();
res.send(await posts.find({}).toArray())
})
images.get('/:filename', (req, res) => {
gfs.files.findOne({ filename: req.params.filename }, (err, file) => {
// Check if file
if (!file || file.length === 0) {
return res.status(404).json({
err: 'No file exists'
});
}
// Check if image
if (file.contentType === 'image/jpeg' || file.contentType === 'image/png') {
// Read output to browser
const readstream = gfs.createReadStream(file.filename);
readstream.pipe(res);
} else {
res.status(404).json({
err: 'Not an image'
});
}
});
});
// LOADING FUNCTION FOR MONGODB
async function load() {
const client = await mongodb.MongoClient.connect
(`${config.dbUrl}`, {
useNewUrlParser: true,
useUnifiedTopology: true
});
return client.db('myCluster').collection('uploads.files');
}
module.exports = images
Every help would be nice and much appreciated! Thanks! :)

ENOENT every time I put a function inside an express.js server

I'm trying to build a simple wrapper for the tesseract.js API so I can use it with a simple GET request within Google Apps Script and I'm having some trouble. For some reason it works perfectly but as soon as I put the same code inside of the express server I keep getting this:
Error: ENOENT: no such file or directory, open
Here's the code:
const Tesseract = require('tesseract.js')
const request = require('request')
const fs = require('fs')
require('custom-env').env();
const express = require('express');
const app = express();
const os = require('os');
const path = require('path');
const PORT = process.env.PORT;
const url = 'http://tesseract.projectnaptha.com/img/eng_bw.png'
app.get('/', function(req, res){
if (req.query.imageUrl) {
const filename = 'pic.png'
const tempFilePath = path.join(os.tmpdir(), 'pic.png');
const writeFile = fs.createWriteStream(tempFilePath);
console.log('got here');
request(url).pipe(writeFile).on('close', function() {
console.log(url, 'saved to', tempFilePath)
Tesseract.recognize(tempFilePath)
.progress(function (p) {})
.catch(err => console.error(err))
.then(function (result) {
res.send(result.text);
process.exit(0);
})
});
fs.unlinkSync(tempFilePath);
} else {
res.send('Send a URL you muppet!');
}
});
Tesseract.recognize is async and returns a promise. Yet you are calling fs.unlinkSync(tempFilePath); immediately after recognize() which will execute before recognize() completes. This race can mean the file is killed before you are done processing.
You should remove the unlink into the then so that you only unlink when complete.
app.get('/', function(req, res){
if (req.query.imageUrl) {
const filename = 'pic.png'
const tempFilePath = path.join(os.tmpdir(), 'pic.png');
const writeFile = fs.createWriteStream(tempFilePath);
console.log('got here');
request(url).pipe(writeFile).on('close', function() {
console.log(url, 'saved to', tempFilePath)
Tesseract.recognize(tempFilePath)
.progress(function (p) {})
.catch(err => console.error(err))
.then(function (result) {
res.send(result.text);
fs.unlinkSync(tempFilePath);
})
});
} else {
res.send('Send a URL you muppet!');
}
});

Resources