Reputation: 13585
I am new to NodeJS and Express framework.
Reason for using Express is that it is one of the best NodeJS web framework available.
Here is my issue:
I have a bitcoinTest.js which prints some results on the screen. Want to display it on a web page.
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');
});
How would I implement last two routes and get details from bitcoinTest.js?
Upvotes: 0
Views: 150
Reputation: 27292
It seems your problem isn't with Express so much as with a lack of understanding of Node. First, I would read about Node modules:
http://nodejs.org/api/modules.html
You could take what you've written and simply export the bitcoin.Client
object and use it from your app file, but you're not actually doing anything but using the bitcoin.Client
methods, so why not put it all in your app file? So your app file would become something like this:
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');
});
If you start adding more Bitcoin functionality (that isn't just calling bitcoin.Client
) methods, then it would make sense to create a module. You could have something like this:
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
}
}
Then you could use it like this:
var myBitcoin = require('./lib/bitcoin.js')({
host: 'localhost',
port: 8332,
user: 'himanshuy',
pass: 'xxxx'
});
// now you can use your custom methods
myBitcoin.myMethod1(/*whatever*/);
Upvotes: 3
Reputation: 3645
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});
}
});
The other user beat me to the answer but he left out the express require statements and my response is slightly different, so ... Like he said, no need to have two separate files.
Upvotes: 0
Reputation: 2625
I don't see much benefit having bitcoinTest as a separate module at the present. if you want to do this you have to do additional wrapper for bitcoin procedures and then set endpoint to them via module.exports.
So my suggestion is to have single 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 notes:
hope you find it useful
Upvotes: 1