我对网络开发相对来说是新手,对node.js来说是全新的。
我试图使用express创建一个使用主厨节点api客户端更新api的站点,使用本教程
如果我创建了一个独立的node.js应用程序,它会像预期的那样工作,并且“培根”值被设置为“很好”。
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)
});完整代码在这里。这个应用程序基本上如下所示:
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();
});路由/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)
});
});竞争/数据库。
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();
});在路由/index.js中使app.js中的app.js变量可用的正确方法是什么?
如果这是正确的做法,那么什么可以使应用程序本身工作,但不是当使用与快速框架?
由于这是我的第一个node.js /快递网站,任何其他的建议,以使这个应用程序的工作将非常感谢。
更新
找到了解决方案,可以在这里获得工作代码片段:https://gist.github.com/spuder/1e39868b6a9a0c3cdb13
发布于 2015-08-07 03:31:30
如果route_index.js是您的routes/index.js,那么您的问题是您实际上没有从它导出任何东西。当您require('routes/index')时,您将在该文件中获取您将module.exports设置为的任何内容。
这意味着您的routes/index.js文件应该以以下内容结尾:
module.exports = router;要解决如何在不使用全局的情况下共享chef_client的问题,可以从routes/index.js返回一个工厂,该工厂使用传递给它的参数返回实例化路由器。看起来是这样的:
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);https://stackoverflow.com/questions/31867287
复制相似问题