node.js 跨文件导出变量

node.js export variables across files

我是 Web 开发的新手,对 node.js 是全新的。

我正在尝试使用 express 创建一个网站,该网站使用 chef-node api client, using this tutorial

更新 api

如果我创建一个独立的 node.js 应用程序,它会按预期工作,并且值 'bacon' 设置为 'good'

app1.js

var fs = require('fs'),
    chef = require('chef'),
    key = fs.readFileSync('/Users/foo.pem'),
    chef_client = chef.createClient('foo', key, 'https://chef.example.com/organizations/dev');

var mybreakfast = { "id":"food","bacon":"good"}
chef_client.put('/data/breakfast/food', mybreakfast,  function(err,res,body) {
  if (err) { return console.log(err); }
  console.log(body)
});

Full code is here。 该应用程序基本上如下所示:
app.js

var fs = require('fs'),
    chef = require('chef'),
    key = fs.readFileSync('/Users/foo.pem'),
    chef_client = chef.createClient('foo', key, 'https://chef.example.com/organizations/dev');
 ...
 //tutorial says this isn't ideal, but it is quickest way to get working
 app.use(function(req,res,next){
     req.client = chef_client;
     next();
 });

routes/index.js

var express = require('express');

var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/databags', function(req, res) {
  req.client.get('/data/breakfast/food',  function(err,res,body) {
    if (err) { return console.log(err); }
    console.log("Found breakfast")
    console.log(body)
    bag_data = body; //TODO: Global variable is bad and you should feel bad, how do I use correct variable?
  });
  res.render('databags.jade', { title: 'Databags', somedata: bag_data });
});

router.post('/databags', function(req, res) {
  var mybreakfast = { "id":"food","bacon":"good"}
  req.client.put('/data/breakfast/food', mybreakfast,  function(err,res,body) {
    if (err) { return console.log(err); }
    console.log(body)
  });

});

vies/databags.玉

html
  body
    form(action='/databags', id='derp', method='POST')
      each value, key in somedata
        label #{key}  
        input(type='text',name="#{key}", value="#{value}")
        br
        br
      input(type='submit', value='Submit', form='derp')

当我按下 /databags 中的提交按钮时,收到 301 并且没有数据上传。

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>openresty/1.7.10.1</center>
</body>
</html>

我怀疑问题与我在每个请求中添加客户端有关。

 //tutorial says this isn't ideal, but it is quickest way to get working
 app.use(function(req,res,next){
     req.client = chef_client;
     next();
 });

使 app.js 中的 chef_client 变量在 routes/index.js 中可用的正确方法是什么?

如果这是执行此操作的正确方法,那么是什么让该应用程序自行运行,而不是与 express 框架一起使用时?

由于这是我的第一个 node.js / express 网站,如有任何其他使该应用正常运行的建议,我们将不胜感激。

更新

已找到解决方案,此处提供工作代码片段:https://gist.github.com/spuder/1e39868b6a9a0c3cdb13

如果 route_index.js 是您的 routes/index.js 那么您的问题是您实际上没有从中导出任何内容。当你 require('routes/index') 时,你将取回你在该文件中设置 module.exports 的任何内容。

这意味着您的 routes/index.js 文件应以:

结尾
module.exports = router;

要解决有关如何在没有全局的情况下共享 chef_client 的问题,您可以 return 来自 routes/index.js 的工厂 return 使用参数的实例化路由器你传递给它。那看起来像这样:

routes/chef.js

function addChefRoutes(router, chefClient) {
  router.get('databags', function(res,req){
    // ...
  }
});

module.exports = addChefRoutes;

app.js

var chefClient = chef.createClient('foo', key, 'https://chef.example.com/organizations/dev');
var addChefRoutes = require('./routes/chef');
var router = express.Router();

addChefRoutes(router, chefClient);