CouchDB 使用 javascript 来验证、查询、进行 map-reduce 等。我想知道有没有办法在这些 javascript 函数中使用 node.js
库?比如使用require('http')
或者用npm
安装的一些第三方模块。
谢谢。
最佳答案
您可以使用 Node.js 库,只要它们不需要 Node.js 特定的库,例如 http
。例如,async在 CouchDB 中工作。经验法则:如果它是为服务器和客户端设计的,你应该是好的。
您可以使用 CommonJS 的 module.exports
和 exports[something]
模式在 View 之间共享代码。查看the documentation了解更多详情。
例如,考虑这个 View :
{
_id:"_design/test",
views: {
lib: {
test: "exports.guests = 42;"
},
fish_per_person: {
map: function(doc){
var guests = require('views/lib/test').guests; // 42
emit(doc.number_of_fish, doc.number_of_fish / guests);
}
}
}
}
fish_per_person
View 需要 lib/test
中导出的值 guests
。
关于javascript - 将 CouchDB 与 Node.js 库一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18485936/