AD authentication in Node js - node.js

I have created a serverside rendering angular application. I'm trying to authenticate the application using #azure/msal-angular - 0.1.4 (AD Authentication). When I run the application using the command
node app.js
ReferenceError: Window is not defined
at Object.<anonymous> (serversiderendering\node_modules\#azure\msal-angular\dist\msal.module.js:25:3)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (serversiderendering\node_modules\#azure\msal-angular\dist\index.js:13:21)
at Module._compile (internal/modules/cjs/loader.js:776:30)
Kindly help me to find out what I'm missing. Or is there any other option to authenticate(Active Directory) serverside rendering application.
Thanks

You can use ldapjs, it's very simple and easy to use.
var ldap = require('ldapjs');
var client = ldap.createClient({
url: 'ldap://127.0.0.1:1389'
});
client.bind('cn=root', 'secret', function(err) {
if(err){console.log(err);}
});

Related

app.use(express.urlencoded({extended:false})) throws error on submit

I had to submit a form containing few parameters from localhost:2000/articles/new. I put the form method and action as <form action="/articles" method="POST">.
My post function is in article route and is like this:
router.post("/", (req, res) => {
console.log("sent")
console.log(req)
res.end()
})
My server.js file contains app.use("/articles", articleRouter)
I have used app.use(express.urlencoded({extended:false})) in server.js file
Now problem arises here:
==> when I use the app.use(express.urlencoded({extended:false})) I get error on submitting form.
The error I got is below:
C:\blog\node_modules\iconv-lite\encodings\dbcs-codec.js:1
SyntaxError: Invalid or unexpected token
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\blog\node_modules\iconv-lite\encodings\index.js:12:5)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.getCodec (C:\blog\node_modules\iconv-lite\lib\index.js:65:27)
at Object.getDecoder (C:\blog\node_modules\iconv-lite\lib\index.js:127:23)
==>But when I don't use it and submit the form the console.log of post function (router.post function ) works. I do not know what is the problem here please help me.
PROBLEM SOLVED FOR ME
I simply deleted node modules folder and again ran npm i on cmd.
This reinstalled all my node modules and its file, now it is working for me.

Datastore is not a contructor error when trying ./bin/www

I received an error when I try to execute the command "./bin/www" in my installed ubuntu application on Windows.
Error is as below:
/mnt/c/Users/admin/Documents/html/developer_portal/app.js:91
dataset: new Datastore(datastoreOptions)
^
TypeError: Datastore is not a constructor
at Object.<anonymous> (/mnt/c/Users/admin/Documents/html/developer_portal/app.js:91:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/mnt/c/Users/admin/Documents/html/developer_portal/bin/www:7:11)
at Module._compile (module.js:652:30)
I ran into the same issue trying to re-use some older code with v3.0 of the node helper library for Datastore. It looks like the import syntax has changed a bit in v3. Line 91 in your app.js probably looks something like:
const datastore = new Datastore({projectID: 'myProject'});
Just before that you probably declare Datastore like:
const Datastore = require('#google-cloud/datastore');
... when what you now want is:
const {Datastore} = require('#google-cloud/datastore');

ReferenceError: require is not defined (node.js)

I'm trying to run a node.js script from my node.js server. The problem is that the modules I import with require () do not work anymore while all the modules and files are in the same folder.
var mysql = require('mysql');
^
ReferenceError: require is not defined
at evalmachine.<anonymous>:3:13
at ContextifyScript.Script.runInThisContext (vm.js:25:33)
at Object.runInThisContext (vm.js:97:38)
at Object.<anonymous> (D:\wamp\www\node_server\test_server.js:8:14)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
Thank you in advance,
I had a similar problem - server-side nodejs thinking that require is not defined.
It turns out the line "type": "module" in my package.json was causing this error, and once I removed that require worked as expected.

Type Error: require(...) is not a function in SendGrid NodeJS

I am trying to send emails using the following doc on sendrid:
https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/nodejs.html
My app.js looks like this:
var sendgrid = require('sendgrid')('SENDGRID_APIKEY');
When runnig npm start I get the following error:
var sendgrid = require('sendgrid')(conf);
^
TypeError: require(...) is not a function
at Object.<anonymous> (C:\Users\Grandullon\Desktop\angular\app.js:8:36)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Users\Grandullon\Desktop\angular\bin\www:7:11)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
Any ideas what I am doing wrong? conf is a variable where I store my API key.
I am using a clean express installation.
THANKS
My package.json can be looked here as requested:
http://pastebin.com/pGKWFReX
The latest documentation shows a different structure for initialization.
You should try doing
var sg = require('sendgrid').SendGrid(conf);

Code of nodejs using mongoose works on local machine but on VPS it doen't

This works on windows 7 localhost:
var User = mongoose.model(userDBName, userSchema);
But it gives this error for Debian VPS:
/home/node_modules/mongoose/lib/utils.js:28
return pluralize(name.toLowerCase());
^
TypeError: Cannot call method 'toLowerCase' of undefined
at exports.toCollectionName (/home/node_modules/mongoose/lib/utils.js:28:25)
at Mongoose.model (/home/node_modules/mongoose/lib/index.js:361:46)
at Object.<anonymous> (/home/XXX/nodejs/js/dbbase.js:54:21)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/XXX/nodejs/js/db_read.js:1:76)
How to fix it?
The error was the name is really undefined, due to userDBName is undefined. It relative with 'nconf' and config.json. Just incorrect way to this file.

Resources