我是NodeJS和Express框架的新手。使用Express的原因是它是最好的NodeJS web框架之一。以下是我的问题:
我有一个bitcoinTest.js,它在屏幕上打印一些结果。想要在网页上显示它。
bitcoinTest.js
var bitcoin = require('bitcoin');
var client = new bitcoin.Client({
host: 'localhost',
port: 8332,
user: 'himanshuy',
pass: 'xxxx'
});
client.getDifficulty(function(err, difficulty) {
if(err) {
return console.error(err);
}
console.log('Difficuly: ' + difficulty);
});
client.getInfo(function(err, info) {
if(err) return console.log(err);
console.log('Info: ', info);
});server.js
var express = require('express');
var app = express();
app.listen(3000);
app.get('/', function(req, res){
res.send('Hello World');
});
app.get('/getDifficulty', function(req, res){
res.send('Get detail from BitcoinTest file');
});
app.get('/getInfo', function(req, res){
res.send('Get detail from BitcoinTest file');
});我将如何实现最后两条路由并从bitcoinTest.js获取详细信息?
发布于 2014-02-03 23:36:50
看来你的问题与其说是对Express的问题,不如说是对Node缺乏了解。首先,我会阅读关于Node模块的文章:
http://nodejs.org/api/modules.html
您可以使用您编写的内容,只需导出bitcoin.Client对象并从您的应用程序文件中使用它,但实际上除了使用bitcoin.Client方法之外,您并没有做任何事情,那么为什么不将它们全部放到应用程序文件中呢?所以你的应用程序文件会变成这样:
var http = require('http'),
express = require('express'),
bitcoin = require('bitcoin');
var app = express();
var client = new bitcoin.Client({
host: 'localhost',
port: 8332,
user: 'himanshuy',
pass: 'xxxx'
});
app.get('/', function(req, res){
res.send('Hello World');
});
app.get('/getDifficulty', function(req, res){
client.getDifficulty(function(err, difficulty) {
if(err) {
res.send('Bitcoin error: ' + err);
} else {
res.send('Difficulty: ' + difficulty);
}
});
}
app.get('/getInfo', function(req, res){
client.getInfo(function(err, info) {
if(err) {
res.send('Bitcoin error: ' + err);
} else {
res.send('Info: ' + info);
});
});
http.createServer(app).listen(3000, function(){
console.log('Server started on port 3000');
});如果您开始添加更多的比特币功能(这不仅仅是调用bitcoin.Client)方法,那么创建一个模块就有意义了。你可以有这样的东西:
lib/bitcoin.js
var bitcoin = require('bitcoin');
module.exports = function(options){
var client = new bitcoin.Client({
host: options.host,
port: options.port,
user: options.user,
pass: options.pass,
});
return {
myMethod1: function(){
// uses client and returns something useful
},
myMethod2: function(){
// uses client and returns something useful
}
}然后你就可以像这样使用它:
var myBitcoin = require('./lib/bitcoin.js')({
host: 'localhost',
port: 8332,
user: 'himanshuy',
pass: 'xxxx'
});
// now you can use your custom methods
myBitcoin.myMethod1(/*whatever*/);发布于 2014-02-03 23:36:13
我认为,目前将bitcoinTest作为一个单独的模块没有多大好处。如果你想这样做,你必须为比特币过程做额外的包装,然后通过module.exports为他们设置端点。
所以我的建议是使用单一的server.js
var bitcoin = require('bitcoin');
var client = new bitcoin.Client({ // use nconf to set parameters
host: 'localhost',
port: 8332,
user: 'himanshuy',
pass: 'xxxx'
});
app.get('/', function(req, res){
res.send('Hello World');
});
app.get('/getDifficulty', function(req, res){
client.getDifficulty(function(err, difficulty) {
if(err) {
res.send("ERROR");//log error
} else {
res.send(difficulty);
}
});
});
app.get('/getInfo', function(req, res){
client.getInfo(function(err, info) {
if(err) {
res.send("ERROR"); //log error
} else {
res.send(info);
}
});
};2注:
希望你觉得有用
发布于 2014-02-03 23:38:16
var express = require('express');
var bitcoin = require('bitcoin');
var client = new bitcoin.Client({
host: 'localhost',
port: 8332,
user: 'himanshuy',
pass: 'xxxx'
});
var app = express();
app.listen(3000);
app.get('/', function(req, res){
res.send('Hello World');
});
app.get('/getDifficulty', function(req, res){
client.getDifficulty(function(err, difficulty) {
if(err) {
return console.error(err);
}
console.log('Difficuly: ' + difficulty);
res.send(200 {'Difficuly: ' + difficulty});
}
});
app.get('/getInfo', function(req, res){
client.getInfo(function(err, info) {
if(err) return console.log(err);
console.log('Info: ', info);
res.send(200, {'Info: ', info});
}
});另一个用户的回答比我快,但他忽略了快递的要求语句,我的反应略有不同,所以.就像他说的,没必要有两个单独的文件。
https://stackoverflow.com/questions/21539835
复制相似问题