首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >节点JS:快速入门

节点JS:快速入门
EN

Stack Overflow用户
提问于 2014-02-03 23:15:40
回答 3查看 144关注 0票数 0

我是NodeJS和Express框架的新手。使用Express的原因是它是最好的NodeJS web框架之一。以下是我的问题:

我有一个bitcoinTest.js,它在屏幕上打印一些结果。想要在网页上显示它。

bitcoinTest.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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获取详细信息?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-02-03 23:36:50

看来你的问题与其说是对Express的问题,不如说是对Node缺乏了解。首先,我会阅读关于Node模块的文章:

http://nodejs.org/api/modules.html

您可以使用您编写的内容,只需导出bitcoin.Client对象并从您的应用程序文件中使用它,但实际上除了使用bitcoin.Client方法之外,您并没有做任何事情,那么为什么不将它们全部放到应用程序文件中呢?所以你的应用程序文件会变成这样:

代码语言:javascript
复制
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

代码语言:javascript
复制
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
        }
    }

然后你就可以像这样使用它:

代码语言:javascript
复制
var myBitcoin = require('./lib/bitcoin.js')({
    host: 'localhost',
    port: 8332,
    user: 'himanshuy',
    pass: 'xxxx'
});

// now you can use your custom methods
myBitcoin.myMethod1(/*whatever*/);
票数 3
EN

Stack Overflow用户

发布于 2014-02-03 23:36:13

我认为,目前将bitcoinTest作为一个单独的模块没有多大好处。如果你想这样做,你必须为比特币过程做额外的包装,然后通过module.exports为他们设置端点。

所以我的建议是使用单一的server.js

代码语言:javascript
复制
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注:

  1. 不要设置具有硬编码值的client/database/etc,使用nConf模块设置数据,如pass、config.json名称
  2. 使用温斯顿模块提供的adv.logging。

希望你觉得有用

票数 1
EN

Stack Overflow用户

发布于 2014-02-03 23:38:16

代码语言:javascript
复制
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});
        }
});

另一个用户的回答比我快,但他忽略了快递的要求语句,我的反应略有不同,所以.就像他说的,没必要有两个单独的文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21539835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档