Using global config variable in express — Node js framework

帅比萌擦擦* 提交于 2019-12-12 22:30:40

问题


I am working with amazon aws server and writing an application to access my instances. But I am not able to pass around the amazon global config variable to the javascript file that gets executed when the button is clicked.

This is my structure --

app.js -.

var express = require('express')
, http = require('http')
, us = require('underscore')
, aws =  require('aws-sdk')
, fs = require('fs');

....

var contents = fs.readFileSync("aws_config/LAUNCH.config.json");
var launch_config = JSON.parse(contents);

var contents = fs.readFileSync("aws_config/AWS_CREDENTIALS.config.json");
var aws_config = JSON.parse(contents);

aws.config.update({
   accessKeyId: aws_config.accessKeyId,
   secretAccessKey: aws_config.secretAccessKey,
   region: aws_config.region 
});

...

app.get('/', function(req, res){
res.render("index.jade",{aws_config_file : aws_config, 
                       launch_config_file : launch_config,
                       aws_var : aws});
});

...

This the point where I show some HTML in the index page and and have an form with button which gets the status of the instance from the server. So

index.jade

  p.buttons
  input(type = "button", style='value = 'Check' , onclick = "t(aws_config_file,launch_config_file,aws_var)")

Now in my t() function in another file I use the amazon API ..

t(aws_config_file,launch_config_file,aws) {
   **var ec2 = new aws.EC2();**
   **var autoscaling = new aws.AutoScaling();**

   perform the check.. call the aws call back provided in the API.

}

How can I use the aws variable that I defined in the app.js file in the function t(). I tried declaring the aws variable in function t .. even that does not work.

Can someone help.

Thanks

来源:https://stackoverflow.com/questions/17843787/using-global-config-variable-in-express-node-js-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!