I am using Express-Middleware in an outsourced file.
I set some session data in that outsourced file:
req.session.userId
Now I want to access this variable in my root file (app.js), but there it is NULL.
How can I make this session data accessible from everywhere?
Okay. You simply declare the variable as global:
./app.js
global.session = { }
Now you can reach this object from everywhere:
global.session.userId = 1
As seen here:
Global session variable in express.js route?
Related
In my Vue app, I am generating links within the same domain, i.e.:
const participantLink = computed(() => { return `${process.env.VUE_APP_SERVER_DOMAIN}${participantStore.shareLinkPath}` })
As you can see, I use an environmental variable for the server domain. For example, in my local development environment, I have set:
VUE_APP_SERVER_DOMAIN=http://localhost:8080
I need to programmatically set this environmental variable for Heroku review apps, because their domain varies. I have set the review app URL pattern to be predictable using an identifier + pull request number pattern, e.g. my-app-pr-123.herokuapp.com.
I can access Heroku injected environmental variables and would like to programmatically set the application domain using these for the review apps. This Stack Overflow post suggests that I should be able to set something like:
"https://my-app-pr-#{HEROKU_PR_NUMBER}.herokuapp.com"
I tried this and the computed value for participantLink above was literally
"https://my-app-pr-#{HEROKU_PR_NUMBER}.herokuapp.com"
I tried the following, and in each case the exact string was returned:
https://my-app-pr-${process.env.HEROKU_PR_NUMBER}.herokuapp.com
`https://my-app-pr-${process.env.HEROKU_PR_NUMBER}.herokuapp.com`
"https://my-app-pr-${process.env.HEROKU_PR_NUMBER}.herokuapp.com"
https://my-app-pr-${HEROKU_PR_NUMBER}.herokuapp.com
`https://my-app-pr-${HEROKU_PR_NUMBER}.herokuapp.com`
"https://my-app-pr-${HEROKU_PR_NUMBER}.herokuapp.com"
I settled on writing the following into the code:
const participantLink = computed(() => {
const modifier = process.env.HEROKU_PR_NUMBER
? process.env.HEROKU_PR_NUMBER
: null
const link = modifier
? `community-visioning-pr-${modifier}.herokuapp.com`
: process.env.VUE_APP_SERVER_DOMAIN
console.log(`${link}${participantStore.shareLinkPath}`)
return `${link}${participantStore.shareLinkPath}`
})
But it is tedious and unreasonable to have to specify this for every review app.
How might I programmatically set the app domain for a review app on Heroku?
So, I was attempting to do something like the following:
if(Meteor.isServer){
Meteor.methods({connect_to_api: function(vars){
// get data from remote API
return data;
}});
}
if(Meteor.isClient){
Template.myTpl.content = function(){
Meteor.call('connect_to_api', vars, function(err,data){
Session.set('placeholder', data);
});
return Session.get('placeholder');
};
}
This seemed to be working fine, but, of course, now breaks in 0.5.9 as the Session object has been removed from the server. How in the world do you now create a reactive Template that uses a server-only (stuff we don't want loading on the client) method call and get data back from that Method call. You can't put any Session references in the callback function because it doesn't exist on the server, and I don't know of any other reactive data sources available for this scenario.
I'm pretty new to Meteor, so I'm really trying to pin down best-practices stuff that has the best chance of being future-proof. Apparently the above implementation was not it.
EDIT: To clarify, this is not a problem of when I'm returning from the Template function. This is a problem of Session existing on the server. The above code will generate the following error message on the server:
Exception while invoking method 'connect_to_api' ReferenceError: Session is not defined
at Meteor.methods.connect_to_api (path/to/file.js:#:#)
at _.extend.protocol_handlers.method.exception ... etc etc
Setting the session in the callback seems to work fine, see this project I created on github: https://github.com/jtblin/meteor_session_test. In this example, I return data in a server method, and set it in the session in the callback.
There are 2 issues with your code:
1) Missing closing brace placement in Meteor.methods. The code should be:
Meteor.methods({
connect_to_api: function(vars) {
// get data from remote API
return data;
}
});
2) As explained above, you return the value in the session, before the callback is completed, i.e. before the callback method had the time to set the session variable. I guess this is why you don't see any data in the session variable yet.
I feel like an idiot (not the first time, not the last). Thanks to jtblin for showing me that Session.set does indeed work in the callback, I went back and scoured my Meteor.method function. Turns out there was one spot buried in the code where I was using Session.get which was what was throwing the error. Once I passed that value in from the client rather than trying to get it in the method itself, all was right with the world.
Oh, and you can indeed order things as above without issue.
I got custom.php (inside config folder) with code like below
$config['myurl'] = 'somesite.com';
I got config.php ( standard CI ), I want to set the base_url using the value from custom.php, like this
$config['base_url'] = $this->config->item('myurl');
Doing that I got the error
Using $this when not in object context in /Volumes/HD 2/work/vnl/app/config/config.php on line 18
Whats the right code for this purpose?
This is not going to work there. The $this that you would want to use there is a controller instance and that is not created yet when the standard application/config/config.php loads.
You can try to add a pre_system hook (that's loaded right before the config object) and include in some helper function that can be called and return the desired value in the application/config/config.php. The usual constants like APPPATH will be available then.
If you can make this config variable a constant that can work too (just put the define() inside application/config/constants.php). Since the config file is just a regular php source file, you can have conditionals here too if you must.
You can just work with the arrays.
In your custom config file
$config['myurl'] = 'myurl.com';
$config['base_url'] = &$config['myurl'];
This will change the default base_url once the custom config file is loaded, you can also just reset the base_url element in your custom config, however in my opinion it is not recommended to reset the base url in another config file as later in the project you could lose track of where it is set, why not set it (and maybe use conditions) in the main config file?
I need to change the db name for particular login in (parameter . ini file) in symfony 2.0. So i tried in session .But it is not working .Is it possible to put session value in parameter . ini file ?
My code: (parameter . ini File)
<?php
session_start();
$dbnamenew='';
if($_SESSION['test_db']!=""){
$dbnamenew=$_SESSION['test_db'];
}
else {
$dbnamenew ='test';
}
?>
; These parameters can be imported into other config files
; by enclosing the key with % (like %database_user%)
; Comments start with ';', as in php.ini
[parameters]
database_driver="pdo_mysql"
database_host="localhost"
database_port="22"
database_name="<?php echo dbnamenew;?>"
database_user="user"
database_password="pass"
mailer_transport="smtp"
mailer_host="localhost"
mailer_user=""
mailer_password=""
locale="en"
secret="b538e3680321a85b2e39a3d1772e0b711ff9371c"
Even if you could use PHP in the configuration file it wouldn't work since the parsed configuration is cached (so it's only executed once).
If you have a finite number of databases, define a database connection for each one. You can later choose it based on the variable taken from the session.
If you use doctrine's entity manager as well, or if you have infinite number of databases, you'll have to alter the connection parameters on the fly. You can do it with an event listener.
Related answer describing how to change the database connection: Symfony 2 : multiple and dynamic database connection
I plan to write the Global.asa file in JavaScript. When a session or application ends, I need changes to be made in a database. Specifically, I need to break up a string and use it in a query in the Global.asa functions. That part doesn't puzzle me at all. Just the part about passing a session variable in as a parameter. I imagine my Global.asa to look something like this:
(Let's say the two variables "variable1" and "variable2" were session variables)
<script language="JScript" runat="Server">
function Application_OnStart() {
}
function Application_OnEnd(variable1) {
}
function Session_OnStart() {
}
function Session_OnEnd(variable2) {
}
</script>
So, I'm not sure if what I'm asking is even feasible. If so, any tips? Keep in mind that I am working in ASP, not ASP.NET. Also, I'm a bit new to using server-side code, so pardon my ignorance.
No, what you suggest is not feasible.
The functions in the global.asa file are callbacks for events. The functions get called, all with no parameters.
You can make use of the Session object to do what you want.
In your code you can set a session variable like this:
session("userid") = 856
In your global.asa you can then use:
variable1 = session("userid")
Have you considered using VBScript for your Global.asa? I've never coded a Global.asa in javascript, but you could code it in VB and use Session or Application level variables:
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart()
'initialize application level variables
Application("ConnectionString") = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.mappath("access_db/mydb.mdb")
End Sub
Sub Application_OnEnd()
Application("ConnectionString") = ""
End Sub
Sub Session_OnStart()
'initialize session level variables
Session("UserIP") = Request.ServerVariables("REMOTE_ADDR")
End Sub
Sub Session_OnEnd()
Session("UserIP") = ""
End Sub
Not sure if you can pass a variable into the predefined functions as such, but you can use Session or Application variables anywhere on the website, including within these functions.