I installed validator module via npm.I am loading the module via this code:
var check = require('validator').check,
sanitize = require('validator').sanitize;
It loads perfectly but when I run this line
data.message = sanitize(data.message).escape();
I am getting "TypeError: undefined is not a function" for this line.How can I resolve it ?
Found this library that you might want to use instead:
https://www.npmjs.org/package/google-caja
https://github.com/superkhau/node-google-caja
I read that you need specifically an xss filter. So, directly from the github main page of Validator:
XSS Sanitization
XSS sanitization was removed from the library in 2d5d6999.
For an alternative, look at Yahoo's xss-filters library.
From xss-filters tutorial
Firstly, import:
var xssFilters = require('xss-filters');
Then escape it:
data.message = xssFilters.inHTMLData(data.message);
Related
can some one explain, I am confused by the bodyparser library in nodeJS.
There is this line
var deprecate = require('depd')('body-parser')
If I look for IDE definitions for deprecate, it points to the depd library, but I am not sure what the second (body-parser) parameter doing here?
The line you ask about "activates" depd library for body-parser library.
You can get more details here: https://www.npmjs.com/package/depd
Anyway, if you use express, body-parser is embedded and you do not need to require it. For more details: https://expressjs.com/en/resources/middleware/body-parser.html
All you're doing is calling a function twice. You need to use the "&&" operator to run seperate functions:
var deprecate = require('depd') && require('body-parser')
The official documentation for assert lists many more methods than I am seeing in NodeJS 13.2.
const assert = require('assert');
assert.match('f01', /.\d\d/); // Exception 'TypeError: assert.match is not a function'
Why are things like match() missing?
The match method is missing in Node.js v13.2. You can see the documentation for version 13.2 here.
Node.js no longer supports assert.match(). Sorry!
A quick fix is to use the regex.test(), together with assert.ok() like this:
let html = "<html><body><h1>Welcome</h1>...</body></html>";
assert.ok(/<h1>Welcome<\/h1>/i.test(html), "Incorrect title");
For your example, try this:
assert.ok(/.\d\d/.test('f01'));
I'm trying to build a nodeJS tool to help me analyzing another AngularJS source code.
The idea is to :
read some of the angular project javascript files
for each file, grab the content
eval the content from the file
do some stuff
The problem I'm facing is that my Angular source code uses es6 features like import, export, arrow functions, ...Etc. and I using nodeJS which does not support these features yet.
So I tried to use #babel/core transform() from my Node app code, but it doesn't work. I keep getting error like Unexpected identifier which means it doesn't understand the import {stuff} from 'here'; syntaxe.
srcFiles.forEach(content => {
try {
(function() {
eval(require("#babel/core").transform(content.text).code)
}.call(window, angular));
} catch (e) {
console.log(e);
}
});
An sample test file :
import _ from 'loadash';
console.log("I'm a file with import and export");
export const = 42;
Any idea how I can get this stuff working ? Or maybe another approach ?
You can pass options as the second parameter of transform method. See examples here
I currently have JavaScript dotted around my XPages - I would like to create a JavaScript library containing all this code and then just call the individual functions e.g. the press of a button. Here is an example of the code I would like in the JavaScript library:
// get the user document for that person
var myView:NotesView = database.getView("xpBenutzerInnen");
var query = new java.util.Vector();
query.addElement(sessionScope.notesName);
var myDoc:NotesDocument = myView.getDocumentByKey(query, true);
When I place this code in the library I get the error:
Syntax error on token ":", ; expected
I assume that the "var name:type" declaration is specific to XPages (this is what I found on creating JavaScript vars: http://www.w3schools.com/js/js_variables.asp) - I could just remove the type declaration and the code seems to run without any problems - I would like to better define the variable type though.
Is there any way that I can move the code out of the XPage but still keeping my typing?
Thanking you in advance
Ursus
You need to distinguish between client side JavaScript and Server side JavaScript. Your code is server side JS and should work in a Script library just as it does inside an XPage. Have you accidentally created a client side JS lib?
A few pointers: I try to make functions in a script library independent from global objects. Mostly the database object. This function works in a library just fine:
function getUserDocument(username:string, db:database) {
var myView:NotesView = db.getView("xpBenutzerInnen");
var query = new java.util.Vector();
query.addElement(username);
var myDoc:NotesDocument = myView.getDocumentByKey(query, true);
myView.recycle();
return myDoc;
}
Let me know how it goes
I'm thoroughly confused on how to use an npm module in Meteor client code.
I understand modules like fs would only work server-side, but in this case I'd like to use a simple text module like this for displaying pretty dates:
https://github.com/ecto/node-timeago
I've tried installing the module under /public/node_modules,
and it works great on the server-side following these instructions from SO: (
How do we or can we use node modules via npm with Meteor?)
Meteor.startup(function () {
var require = __meteor_bootstrap__.require
var timeago = require('timeago')
console.log(timeago(new Date()))
...
However it doesn't work in the client-side code:
if (Meteor.is_client) {
var require = __meteor_bootstrap__.require
var timeago = require('timeago')
console.log(timeago(new Date()))
...
Uncaught ReferenceError: __meteor_bootstrap__ is not defined"
Server-side is sort of useless for me in this case, as I'm trying to render text on the client.
I don't believe you need to use the server side version. Use the npm stuff for server side only and btw, put it in your /public/ as well. Who knows maybe you can call it once it is in your /public/, try it. Or try this.
Use something like the jquery timeago.js
Put it in /client/ or something like /client/js
Create a /client/helpers.js or some such.
Use a handlebars helper.
Handlebars.registerHelper('date', function(date) {
if(date) {
dateObj = new Date(date);
return $.timeago(dateObj);
}
return 'a long long time ago in a galaxy far away';
});
Example of calling 'date' handlebars helper function from template.
{{ date created }}
Where date is the handebars helper and created is the date coming out of the meteor/mongo collection.
See the github Britto project. That is where I got this code snippet and used it in a chat room app I wrote. Works fine.
There are a couple of others floating out there. Go to madewith.meteor.com and peruse the source of some of the projects.