Express: Minify CSS/JS from remote URL with UglifyJS - node.js

I managed to get the data on #1, but I can't print it out in Express #2, how can I resolve the data and output it?
I believe this is my code error where I can't place the correct async/await.
Here's my code:
minify.js
const { getContent } = require('./get');
const { minifyJS } = require('./processing');
async function getMinify(req, res) {
try {
const remoteUrl = 'http://example.com/script.js';
console.log('URL: ' + remoteUrl);
// #1
const content = await getContent(remoteUrl);
// #2
const outputJS = await minifyJS(content);
res.end(outputJS);
} catch (error) {
res.end(error.content);
}
}
module.exports = { getMinify }
get.js
const got = require('got');
async function getContent(remoteUrl) {
const code = await got(remoteUrl);
return code.body;
}
module.exports = { getContent }
processing.js
const { minify } = require('uglify-js');
async function minifyJS(data) {
const result = await minify(data);
return result.code;
}
module.exports = { minifyJS }
app.js
const express = require('express');
const { getMinify } = require('./minify');
const app = express();
app.get('/*', getMinify);
app.listen(5000);

based on uglify-js, result of minify(data) function has two object code and error.
and minify(data) don't need to await
processing.js should changed
const { minify } = require("uglify-js");
async function minifyJS(data) {
let { error, code } = minify(data);
if (error)
throw error;
return code;
}
module.exports = { minifyJS };
and for handling of two .js file (get.js and processing.js) changed
minify.js
try {
const remoteUrl = 'http://example.com/script.js';
console.log('URL: ' + remoteUrl);
// #1
const content = await getContent(remoteUrl);
// #2
const outputJS = await minifyJS(content);
res.end(outputJS);
} catch (error) {
res.end(error); // remove .content
}

Related

Async function to scrape subreddits using Cheerio returns undefined

The script by itself works great (entering the url manually, writing a json file using the fs module, node script_name.js) but within a Express get request it returns undefined.
So I've built a simple frontend to let the user enter the subreddit name to be scraped.
And here's where the problem is:
Express controller
const run = require("../run");
requestPosts: async (req, res) => {
try {
const { subreddit } = req.body;
const response = await run(subreddit);
//console.log(response);
res.json(response);
} catch (error) {
console.error(error);
}
},
Cheerio functions
const axios = require("axios");
const { load } = require("cheerio");
let posts = [];
async function getImage(postLink) {
const { data } = await axios(postLink);
const $ = load(data);
return $("a.post-link").attr("href");
}
async function run(url) {
try {
console.log(url);
const { data } = await axios(url);
const $ = load(data);
$(".thing.linkflair.link").map(async (i, e) => {
const title = $(e)
.find(".entry.unvoted .top-matter .title .title")
.text();
const user = $(e)
.find(".entry.unvoted .top-matter .tagline .author")
.text();
const profileLink = `https://old.reddit.com/user/${user}`;
const postLink = `https://old.reddit.com/${$(e).find("a").attr("href")}`;
// const thumbail = $(e).find("a img").attr("src");
const image = await getImage(postLink);
posts.push({
id: i + 1,
title,
postLink,
image,
user: { user, profileLink },
});
});
const nextPage = $(".next-button a").attr("href");
if (nextPage) {
await run(nextPage);
} else {
return posts;
}
} catch (error) {
console.error(error);
}
}
module.exports = run;
I've tried working with Promise((resolve, reject) => {}).
I think it's returning undefined because maybe the code its not synchronized.
(idk if it makes sense, i've just started programming)
.map() is not promise-aware and does not wait for your promises to finish. So, $(".thing.linkflair.link").map() finishes long before any of the asynchronous functions inside its callback do. Thus you try to return posts BEFORE it has been populated.
Passing an async callback to .map() will return an array of promises. You can use Promise.all() on those promises to know when they are done and once you're doing that, you may as well just return each post object rather that using a higher level scoped/shared object, thus making the code more self contained.
I would suggest this code:
async function run(url) {
try {
console.log(url);
const { data } = await axios(url);
const $ = load(data);
const posts = await Promise.all($(".thing.linkflair.link").map(async (i, e) => {
const title = $(e)
.find(".entry.unvoted .top-matter .title .title")
.text();
const user = $(e)
.find(".entry.unvoted .top-matter .tagline .author")
.text();
const profileLink = `https://old.reddit.com/user/${user}`;
const postLink = `https://old.reddit.com/${$(e).find("a").attr("href")}`;
// const thumbail = $(e).find("a img").attr("src");
const image = await getImage(postLink);
// return a post object
return {
id: i + 1,
title,
postLink,
image,
user: { user, profileLink },
};
}));
const nextPage = $(".next-button a").attr("href");
if (nextPage) {
const newPosts = await run(nextPage);
// add these posts to the ones we already have
posts.push(...newPosts);
}
return posts;
} catch (error) {
console.error(error);
}
}

NodeJS+Express+SQL Server backend application Error : Incorrect Syntax near '83' , Incorrect Syntax near '68' etc

In my application I get the following errors when I'm trying to reach GET, PUT, DELETE routes (tried POSTMAN and VSCode REST Server):
Error 1:
GET by ID : `Incorrect Syntax near '83'.`
Error 2:
GET ALL : `Incorrect Syntax near '83'.`
Error 3:
PUT : `Incorrect syntax near '85'.`
Error 4:
DELETE : `Incorrect syntax near '68'.`
I have googled the error but most of them are related to spacing errors in parameters in the SQL query but I have checked mine and the queries I have are all correct compared to those.
I suspect if this related to any SQL Server related encoding but not sure.
Below are some of my coding as a start to investigate:
eventController.js
'use strict';
const eventData = require('../data/events');
const getEvents = async (req, res, next) => {
try {
const events = await eventData.getEvents();
res.send(events);
}catch (error) {
res.status(400).send(error.message);
}
}
const getEvent = async (req, res, next) => {
try {
const eventId = req.params.id;
const oneEvent = await eventData.getById(eventId);
res.send(oneEvent)
} catch (error) {
res.status(400).send(error.message)
}
}
const updateEvent = async (req, res, next) => {
try {
const eventId = req.params.id;
const data = req.body;
const updated = await eventData.updateEvent(eventId, data);
res.send(updated);
} catch (error) {
res.status(400).send(error.message)
}
}
const deleteEvent = async (req, res, next) => {
try {
const eventId = req.params.id;
const deletedevent = await eventData.deleteEvent(eventId);
res.send(deletedevent);
} catch (error) {
res.status(400).send(error.message)
}
}
module.exports = {
getEvents,
getEvent,
updateEvent,
deleteEvent
}
eventslist.sql
SELECT [ord_type],
[ord_no],
[line_seq_no],
[lvl_no],
[cmt_type],
[cmt_seq_no],
[cmt],
[CMT_DOC_TYPE],
[EXTRA_1],
[extra_2],
[extra_3],
[extra_4],
[extra_5],
[extra_6],
[extra_7],
[extra_8],
[extra_9],
[extra_10],
[extra_11],
[extra_12],
[extra_13],
[extra_14],
[extra_15],
[FILLER_0001],
[ID],
[is_ext],
[RowVersion]
FROM [100].[dbo].[OELINCMT_SQL]
utils.js
'use strict';
const fs = require('fs-extra');
const {join} = require('path');
const loadSqlQueries = async (folderName) => {
const filePath = join(process.cwd(), 'data', folderName);
const files = await fs.readdir(filePath);
const sqlFiles = await files.filter(f => f.endsWith('.sql'));
const queries = {};
for (const sqlFile of sqlFiles) {
const query = await fs.readFileSync(join(filePath, sqlFile));
queries[sqlFile.replace(".sql", "")] = query
}
return queries;
}
module.exports = {
loadSqlQueries
}
UPDATE : Added data/events
index.js
'use strict';
const utils = require('../utils');
const config = require('../../config');
const sql = require('mssql');
const getEvents = async () => {
try {
let pool = await sql.connect(config.sql);
const sqlQueries = await utils.loadSqlQueries('events');
const list = await pool.request().query(sqlQueries.eventslist);
return list.recordset;
} catch (error) {
return error.message;
}
}
const getById = async (eventId) => {
try {
let pool = await sql.connect(config.sql);
const sqlQueries = await utils.loadSqlQueries('events');
const oneEvent = await pool.request()
.input('eventId', sql.Char(8), eventId)
.query(sqlQueries.eventbyId);
return oneEvent.recordset;
} catch (error) {
return error.message;
}
}
const updateEvent = async (eventId, eventData) => {
try {
let pool = await sql.connect(config.sql);
const sqlQueries = await utils.loadSqlQueries('events');
const update = await pool.request()
.input('eventId', sql.Char(8), eventId)
.input('cmt', sql.NVarChar(4000), eventData.cmt)
.query(sqlQueries.updateEvent);
return update.recordset;
} catch (error) {
return error.message
}
}
const deleteEvent = async (eventId) => {
try {
let pool = await sql.connect(config.sql);
const sqlQueries = await utils.loadSqlQueries('events');
const deleted = await pool.request()
.input('eventId', sql.Char(8), eventId)
.query(sqlQueries.deleteEvent);
return deleted.recordset;
} catch (error) {
return error.message
}
}
module.exports = {
getEvents,
getById,
updateEvent,
deleteEvent
}
eventbyId.sql
SELECT [ord_type]
,[ord_no]
,[line_seq_no]
,[lvl_no]
,[cmt_type]
,[cmt_seq_no]
,[cmt]
,[CMT_DOC_TYPE]
,[EXTRA_1]
,[extra_2]
,[extra_3]
,[extra_4]
,[extra_5]
,[extra_6]
,[extra_7]
,[extra_8]
,[extra_9]
,[extra_10]
,[extra_11]
,[extra_12]
,[extra_13]
,[extra_14]
,[extra_15]
,[FILLER_0001]
,[ID]
,[is_ext]
,[RowVersion]
FROM [100].[dbo].[OELINCMT_SQL]
WHERE [ord_no] = #eventId
Request for GET ALL via POSTMAN
Request for GET by ID via POSTMAN
Request for GET ALL via REST Server
RAW Body of GET ALL request
RAW Body of GET by ID request

Having problem with convert XML to XLSX and download it in Node.js

My problem is that i have to create Converter and this converter takes the XML files and converts it to XLSX and download it. i was able to do the first part where the XML convert to JSON, but i have difficult to do the rest of the code, now i am trying to take the file JSON and convert it to XLSX but my code is not working, i do not know why ?
ControllerXML2Json.js
const { json } = require('body-parser')
const fs = require('fs')
const xml2js = require('xml2js')
let jsonString
async function xml2json() {
try {
const xml = fs.readFileSync('src/data/dataset.xml')
const result = await xml2js.parseStringPromise(xml, { mergeAttrs: true })
jsonString = JSON.stringify(result, null, 4)
//console.log(json)
fs.writeFileSync('./public/data/result.json', jsonString)
//return jsonString
} catch (error) {
console.log(error)
}
}
module.exports = {
convert(req, res) {
let xmlFile = req.files.fileXML
xmlFile.mv('src/data/dataset.xml', function (err) {
if (err) {
return res.status(500).send(err)
} else {
xml2json()
}
})
res.redirect('/json')
}
}
ControllerJson2Xlsx.js
const { json } = require("body-parser");
const fs = require("fs");
const xlsx = require("json-as-xlsx");
const path = require('path');
const fileName = "data.xlsx";
async function generateExcelFromJSON(){
try {
let content = JSON.parse(fs.readFileSync("./data/result.json", "utf-8"));
let newWS = xlsx.utils.book_new();
let newWB = xlsx.utils.json_to_sheet(content);
xlsx.utils.book_append_sheet(newWB, newWS, "data");
await xlsx.writeFile("./public/data/data.xlsx", content);
// xlsx.writeFile(newWB, "./public/data/data.xlsx");
} catch (error) {
console.log(error);
}
}
module.exports = {
download(req, res) {
let jsonFile = req.files.fileJSON;
jsonFile.mv('./data/data.xlsx', function (err) {
if (err) {
return res.status(500).send(err);
} else {
xlsx();
}
});
res.redirect("/xlsx");
},
};

nodeJS share variable between modules

i have a problem with my project, i need sharing the configurations parameters between modules, y have my entry point
app.js
const { requestConfiguracion } = require('./clases/servicios');
( async () => {
const dbConfig = await requestConfiguracion();
})();
servicios.js
const axios = require('axios').default;
const requestConfiguracion = async () => {
try {
const request = await axios.get('http://localhost/config/getConfig');
return request.data;
} catch (error) {
console.error(error)
}
}
module.exports = {
requestConfiguracion,
}
I need that the configuration in dbConfig is available for the other modules.
You can create a separated module to read and export that dbConfig.
// db-config.js
const { requestConfiguracion } = require('./clases/servicios');
let dbConfig = null;
module.exports.getDbConfig = async () => {
if (dbConfig) return dbConfig; // cache dbConfig for next time
dbConfig = await requestConfiguracion();
return dbConfig;
};
And in other modules:
// app.js
const { getDbConfig } = require('./db-config');
async function someFunction() {
const dbConfig = await getDbConfig();
// do something
}
Since your requestConfiguracion() function is an async function, you have to use await every time trying to get that config.

Finding the differences between 2 files in nodejs

I have 2 json files which have static data.
File A.json
{
"data": ["A","B","C","D"]
}
File B.json
{
"data":["A","B","C"]
}
Now I want find the difference between these two files.
I tried using this code to get files by its name
const express = require("express");
const jsonfile = require("jsonfile");
const app = express();
const fs = require("fs");
app.use(express.json());
const BASEPATH = "./data"
app.get("/api/v1/data/:name", async (req,res) => {
const fileName = req.params.name;
const filePath = `${BASEPATH}/${fileName}.json`
try {
const readData = await jsonfile.readFile(filePath);
res.status(200).json(readData);
}catch (e) {
res.status(404).send(e);
}
});
The URL is : localhost:3000/api/v1/data/A
To add data I used,
app.put("/api/v1/data",(req,res) => {
const fileName = req.body.name;
const data = req.body.data;
const filePath = `${BASEPATH}/${fileName}.json`
fs.exists(filePath, async exists => {
if(exists) {
try {
await jsonfile.writeFile(filePath,{data:data});
res.status(200).send();
}catch(e) {
res.send(500).json(e);
}
} else {
try {
await jsonfile.writeFile(filePath,{data:data});
res.status(201).send();
}catch(e) {
res.send(500).json(e);
}
}
})
});
Example of added data:
{ "name":"C", "data":["A","B","Z"]}
URL is: localhost:3000/api/v1/data
you can try like this,
app.get("/api/v1/data/diff", async (req,res) => {
try {
const file1 = req.query.file1;
const file2 = req.query.file2;
console.log(file1,file2)
if(file1 === undefined || file2 === undefined ) {
res.status(401).send("BAD REQUEST. SEND FILE NAMES");
return;
} else {
const filePath1 = `${BASEPATH}/${file1}.json`
const filePath2 = `${BASEPATH}/${file2}.json`
const file1Data = await jsonfile.readFile(filePath1);
const file2Data = await jsonfile.readFile(filePath2);
let difference = file1Data.data.filter(x => !file2Data.data.includes(x));
let difference2 = file2Data.data.filter(x => !file1Data.data.includes(x));
res.status(200).json({diff: [...difference,...difference2]});
}
}catch(e) {
res.status(500).json(e);
}
});
you have to use the url like : localhost:3000/api/v1/data/diff?file1=A&file2=B
the output will be
{
"diff": [
"D"
]
}

Resources