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

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!');
}
});

Related

Downloading JSON file in expressjs and reading it

I have a task where I am given a URL such as https://xyz.json. This URL prompts the downloading of the JSON file into the local. I am now required to read the use this JSON data for further processing. Since I am new to NodeJS and express, I find myself confused about how to achieve this in ExpressJS.
This is what I've tried :
const https = require("https");
const fs = require("fs");
const file = fs.createWriteStream("outputFile.json");
const request = https.get(
"https://xyz.json",
function (response) {
response.pipe(file);
// after download completed close filestream
file.on("finish", () => {
file.close();
console.log("Download Completed");
});
}
);
Here, in the outputFile.json, no data is present
Qn2) Can I periodically download using setTimeOut(). Would it be efficient or is there any better way of caching data to make the application faster?
Thanks in advance!
Here's a sample app that downloads a json triggered when you hit an API route hosted as ExpressJS sever.
const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const axios = require('axios');
const fs = require('fs');
const app = express();
app.use(cors());
app.use(morgan(':method :url :status :user-agent - :response-time ms'));
app.use(bodyParser.json());
app.get('/', async (req, res) => {
try {
const { status, data } = await axios.get('http://52.87.135.24/json-files/events.json'); // Can be replaced by your json url
if (status === 200) {
fs.writeFileSync('data.json', JSON.stringify(data));
res.status(200).json({
success: 'Downloaded file.',
data: data // Comment it if you don't want to send the data back
})
} else {
res.status(404).json({ 'Failed': 'File not found.' })
}
} catch (err) {
console.log(err);
res.status(500).json({ 'Error': 'Internal Server Error' });
}
});
app.listen(process.env.PORT || 3000, function () {
console.log('Express app running on port ' + (process.env.PORT || 3000))
});
And as I mentioned that this download gets triggered every time you make a request on http://localhost:3000 in this case, you can create a client script that acts like a cron job in which you can use the setTimeout or actually, setInterval to download your file periodically.
const axios = require('axios');
setInterval(async () => {
await axios.get('http://localhost:3000/');
}, 5000);
Here's such a script along! :)

how to download and save video using nodejs

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="
}

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'));

Not able to see ipfs node id

Below is my app.js file. When I run the following code it shows "swarm listening on" and "file-path" and then nothing happens.
I am also running daemon on another command prompt listening api on 5001.
I think node is not getting initiated thats why it never becomes ready.
var express = require('express');
var app = express();
var ipfsAPI = require('ipfs-api')
var ipfs = ipfsAPI('localhost', '5001', {protocol: 'http'})
const IPFS = require('ipfs');
// Spawn your IPFS node \o/
const node = new IPFS();
var multer = require('multer');
var upload = multer({ dest: './z000000000'});
var fs = require('fs');
/** Permissible loading a single file,
the value of the attribute "name" in the form of "recfile". **/
var type = upload.single('filer');
app.post('/upload', type, function (req,res) {
/** When using the "single"
data come in "req.file" regardless of the attribute "name". **/
var tmp_path = req.file.path;
console.log(tmp_path);
//
node.on('ready', () => {
node.id((err, id) => {
if (err) {
return console.log(err)
}
console.log(id)
})
var data = fs.readFileSync('./'+tmp_path);
console.log("Synchronous read: " + data.toString());
let files = [
{
path: './'+tmp_path,
content: data
}
]
node.files.add(files, function (err, files) {
if (err) {
console.log(err);
} else {
console.log(files)
}
})
})
//
res.end();
});
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
Use ipfsAPI:
var ipfs = ipfsAPI('localhost', '5001', {protocol: 'http'}
To get the ID of node:
ipfs.id()
.then(res => {
showStatus(`daemon active\nid: ${res.ID}`, COLORS.success)
doYourWork()
})
.catch(err => {
showStatus('daemon inactive', COLORS.error)
console.error(err)
})
Find Documentation here: https://github.com/ipfs/js-ipfs-api

Strange Node await never unblocks

Node v8.3 + Express + using async / await complete source code below where await doesn't return unless I moved the terminal!!
// Express
const express = require('express');
const app = express();
// Lodash
const _ = require('lodash');
// DBR
const dbr = require('./build/Release/dbr');
dbr.initLicense("t0068MgAAAGvV3VqfqOzkuVGi7x/PFfZUQoUyJOakuduaSEoI2Pc8+kMwjrojxQgE5aJphmhagRmq/S9lppTkM4w3qCQezxk=");
// Promisify
const {promisify} = require('util');
const decodeFilePromise = promisify(dbr.decodeFileAsync);
app.get('/', async (req, res) => {
console.log("Received a barcode scan request!");
try {
const oneDimensionType = 0x3FF;
const scannedResults = await decodeFilePromise('test.jpg', oneDimensionType);
const imeiResults = _.uniq(_.map(_.filter(scannedResults, ['format', 'CODE_128']), 'value'));
console.log(`Successfully scanned the image: ${imeiResults}`);
res.send(`IMEI results: ${imeiResults}`);
}
catch (err) {
console.log(`Failed to scan the image: ${err}`);
res.send('Could not scan the barcode!');
}
});
app.listen(3000, () => {
console.log('Example app listening on port 3000!');
});
What do I mean by "unless I moved the terminal" ? Please see the attached GIF: https://youtu.be/HW_MqLzEC9M

Resources