NodeJS Express how to unescape HTML entities for display purposes - javascript

I am building a web application in NodeJS with Express, Angular JS and Google App-Engine Datastore.
I am learning Node. I created a form that "sanitizes" (escapes) user input before inserting into the database. I followed the NodeJS tutorial on the MDN website to create this code:
//Trim and escape all inputs
req.sanitize('requester').escape();
req.sanitize('requester').trim();
req.sanitize('dataowner').escape();
req.sanitize('dataowner').trim();
req.sanitize('requested_filepath_list').escape();
req.sanitize('requested_filepath_list').trim();
The 'requested_filepath_list' is a list of UNIX file paths.
So when a user submits the request, it is stored in the database in the "Escaped" format.
//Escaped data
/top/example/test123.txt
Question: How can I "unescape" the data for display purposes?
//Desired output
/top/example/test123.txt
I tried the unescape function but it does not seem to work, it just returns the same output.
let escape_str = '/top/example/test123.txt';
let unescaped_str = unescape(escape_str);
console.log('unescaped_str: ' + unescaped_str);
//Output
unescaped_str: /top/example/test123.txt
//Desired output
/top/example/test123.txt

I was able to use the 'he' library to achieve this requirement.
Here's a post with the details: What's the right way to decode a string that has special HTML entities in it?
Here's the library. I was able to install it using npm install.
https://www.npmjs.com/package/he
Example solution:
const he = require('he');
let escape_str = '/top/example/test123.txt';
let unescaped_str = he.decode(escape_str);
console.log('unescaped_str ' + unescaped_str);

You can try this :)
const querystring = require('querystring');
querystring.unescape(escape_str);

Related

Is it possible for testcafe to read a variable from another file?

I want to create some automation so the user would have to place a file with some details like their address and then my automation script would pick it up.
I'm comfortable with using the const command in TestCafe to pick up local variables but is there a way if i put a file in like lets say address.txt for it to pick up line1 line2 line3, etc?
So i see the code like this:
const line1;
const line2;
const line3;
and in Address.txt we'd have
$line1 = '1 hello world way'
$line2 = 'line 2'
$line3= 'line 3'
What am i missing to plug it all together? Thanks,
You can use the native facilities of NodeJS. Please refer to the example from our documentation for more details.
This would be better if done through json. Here is a high level approach you could take:
Keep an excel file to get data from users (say, address).
Use xlsx-to-json to convert excel data to json.
Refer this json in your script: const dataFile = require('./data.json');
Parse json and do whatever is needed.

how to query column in SQL database via UPPER case conversion IN NODE.js javascript code..

Hi I've recently updated my SEQUELIZE node package from v3 to v4.
In my code I used to query my user table via the following sytnax
var option = {
where: ["upper(email) = ?", ssousername.toUpperCase()]
};
This would effectively query my user table converting all user emails in DB to uppercase.
When upgrading to V4 I am now given the following error thrown by the above query
Unhandled rejection Error: Support for literal replacements in the `where` object has been removed.
I cannot find any documentation as to how I should syntactically format my former query executed via a node.js backend in order to produce the following SQL query
SELECT UPPER(email) AS email
FROM dashboard."user" WHERE email='user#email.com';
Would love some help , Thanks!
They are working on deprecating all string operators I think. Under querying it seems to suggest doing it like this:
let option = {
where: sequelize.where(
sequelize.fn('upper', sequelize.col('email')),
ssousername.toUpperCase()
),
};

How to get data from Viadeo with X-Ray and NodeJs

So I am trying to scrape some content with node.js x-ray scraping framework. While I can get the content from a single page but for exemple only for one employee I can't get my head around on how to get for all the employees.
Working Exemple but return me the first employee:
const request =require('request');
const Xray=require('x-ray');
var x = Xray();
x('http://www.viadeo.com/fr/company/unicef',
'.pan',[{
name:'.pan-emp-name',
job:'.pan-emp-pos',
since:'.pan-emp-age'
// job:'#profile #overview-summary-current ol'
}]).write('result.json')
Thank you so much
x('http://www.viadeo.com/fr/company/unicef',
'#pan-emp .pan-employees .pan-empployee',[{
company:'#company-info .company-logo-picture',
nom:'.pan-emp-name',
job:'.pan-emp-pos',
depuis:'.pan-emp-age'
// job:'#profile #overview-summary-current ol'
}]).write('result.json')
Working like a charm,
So now my problem is to get the company info

SHA1 varies in java and javascript for same input

Facing an issue while creating SHA1 from javascript and java. The problem is both are different. It is used for validating the client request to web server. That means client send a based64 encoded security key to server and server regenerate the same key and equate both are same. Please find below code for generating secret keys in client and server.
Server
MessageDigest mDigest = null;
try {
mDigest = MessageDigest.getInstance("SHA1");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
String input = value1 + value1 + server_key;
byte[] result = mDigest.digest(input.getBytes());
String secret = Base64.encodeToString(result, Base64.NO_WRAP);
...
//comparison logic goes here
...
Client (java script)
var input = value1 + value2 + server_key;
//http://code.google.com/p/crypto-js/
var hash = CryptoJS.SHA1(input);
var encoded = base64Encode(hash.toString());
//WEB SERVICE INVOCATION FROM JAVASCRIPT GIES HERE.
The values value1, value1, server_key will be available in both client and server. The issue we are facing is, the SHA1 generated in both client and server is not matching. I understand the issue is in java its using getBytes() and in javascript using string value for generating SHA1. The CryptoJS.SHA1 does not support bytearray as parameter. We cannot change the server code as it is used by many client applications. Any help will be much appreciated.
In Java ->
byte[] result = mDigest.digest(input.getBytes());
and in JavaScript ->
var hash = CryptoJS.SHA1(input);.
I belief this is the problem. In java the parameter is a bytearray and output is also a bytearray. But in javascript the parameter is var (string) and return is also var (string). I 've also compared the output of CryptoJS.SHA1 with some online SHA1 generating tools. The comparison is true. I am not an expert in this area. If you can explain more, it will be more helpful.
I managed it to do in another way. My application is a cordova based application. So generated the sha1 and encoded it from java and objC and invoked it using cordova plugins.

create hash equivalent to python call using javascript on browser

I'm trying to create hash for calling thesubdb api they've given the python implementation of their hash generation method.
My project needs me to create hash on the go on browser and then pass it to my php backend and then call subdbapi using php.
But for that first of all I need to create hash of video file on my browser using javascript.
I'm using the new html 5 file api to read local file on users computer and create hash for same.
var md5 = CryptoJS.MD5(binary).toString();
console.log(md5);
above is my javascript code.
This does not match the output of the python code that they've given which is :
def get_hash(name):
readsize = 64 * 1024
with open(name, 'rb') as f:
size = os.path.getsize(name)
data = f.read(readsize)
f.seek(-readsize, os.SEEK_END)
data += f.read(readsize)
return hashlib.md5(data).hexdigest()
i'm sure i'm missing something fundamental in my javascript code.
thanks in advance.

Categories

Resources