I'm using passport for my nodejs application, the session is working fine, I can access to my Object user from my views thanks to my global variable 'res.locals.user = req.user', but when I attempt to access to it from a javascript file located in my public folder/javascripts/file.js, my object user is not defined there.
Thank you in advance.
That's because is a server variable and you can only acces it within your teamplate. One solution is to create a variable in to global scope in your template and later use it in your js file.
For example if you are using jade, in your template you can do :
script.
var myVar = !{myVar};
script(src='yourScript')
Related
I am newly initiated backend devloper. I am working with sailsJS [MVC Framework of Nodejs].
I am having one confusion regarding session access and flow in sailsJS. Please help me to clear this.
I am saving my user in session in AdminController, which i had create using CLI service of sailsJS. But I am not able to use that variable in another controller : InventoryContoller in the same application.I was in impression that session is something that we can use in whole application from anywhere.
I created session in one of app controller : AaminController as follow:
req.session.user = user;
But i am not able to access this session variable outside the AdminController.
I want to use this variable inside whole applicaiton.
I have a sendgrid API key. I placed it into a dot.env file inside a config folder.
Bud when I try to use it to set api key:
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
Node keeps telling me API key does not start with "SG."
If I try to console.log(process.env.SENDGRID_API_KEY) it gives me undefined.
inside dev.env file:
SENDGRID_API_KEY=myapikey
I also have a PORT variable inside this folder and it is used without problems, so it is not a matter of a path to the file. It is not a typo either.
Are you using something like the "dotenv" package to read de environment variables from the "dev.env" file? You can't read environment variables in NodeJS without something like that if I am not mistaken. When I learned how to import this kind of variables I was teached to name the file just ".env".
I'm new on Node.js and Electron.
I already developed a Web View Application via CefSharp.WinForm.
When I used CefSharp, I added window.AppViewport object like this.
chromeBrowser.RegisterAsyncJsObject("AppViewport", new AppViewport(this));
And in web page, I used the AppViewport like this.
if(window.AppViewport !== undefined){
window.AppViewport.setDepth(widthDepthLevel);
}
However, I couldn't find registerAsyncJsObject or registerJjObject in electron.
How can I add the javascript object handler in electron like C# appllication?
Is there anything equivalent with that method?
You need to define a global object on your Main process,
global.yourSharedObj = {some_prop: true};
and with Electron's Remote API, you can access that object in Renderer with something like this:
var remote = require('electron').remote;
console.log(remote.getGlobal('yourSharedObj').some_prop);
Remote API Docs:
https://github.com/electron/electron/blob/master/docs/api/remote.md
I am creating a NodeJS API server that will be delegatiing authentication to an oauth2 server. While I could store the key and secret along with the source code, I want to avoid doing that since it feels like a security risk and it is something that doesn't match the lifespan of a server implementation update (key/secret refresh will likely happen more often).
I could store it in a database or a maybe a transient json file, but I would like to know what are the considered the best practices in the NodeJS world or what is considered acceptable. Any suggestions are appreciated.
One option would be to set environment variables as part of your deployment and then access them in the code from the global process object:
var clientId = process.env.CLIENT_ID
var clientSecret = process.env.CLIENT_SECRET
Since I wanted to provide something that can store multiple values, I just created a JSON file and then read that into a module I called keystore (using ES6 class):
class KeyStore {
load() {
// load the json file from a location specified in the config
// or process.env.MYSERVER_KEYSTORE
}
get (keyname) {
// return the key I am looking for
}
}
module.exports = new KeyStore();
I would ideally want to store the file encrypted, but for now I am just storing it read only to the current user in the home directory.
If there is another way, that is considered 'better', then I am open to that.
I am trying to create a non-javascript version of my web app using ejs on the server side. I pass into the template an object containing the app's state, and at one point I want to build a url using that state object. So basically I want to do something like <%=makeUrl(objectState.data[0])%>
how can I make makeUrl callable from within ejs templates?
Thanks
edit: I know I can pass a function in as a parameter to the template, but is there a better way?
in Express 3, they removed the concept of dynamic helpers. I believe that passing functions into the template via app.locals is in fact the recommended way to do this now. I gather you already know how, but for anybody else with this same question:
in your app.js:
app.locals.myFunc = function(arg){...}
in your template:
<%= myFunc(objectState.data[0]) %>