When I tried the code below with an api ket off http://forecast.io/ it worked. But when I tried it via https://coinkite.com/ I get a 401 unauthorized exception.
I don't understand why it is not going through. I have a separate project file which runs similar coinkite code, but is a command line application, and it gives me the right response.
This is the main point of execution,
/*global require*/
/*global console*/
/*global process*/
/*jslint nomen: true*/
/*global __dirname*/
/*jslint nomen: false*/
// Dependancies
var bodyParser = require('body-parser');
var express = require('express');
var path = require('path');
// Express
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var currentPort = process.env.PORT || 3000;
app.set('port', currentPort);
// Here you set that all templates are located in `/views` directory
/*jslint nomen: true*/
app.set('views', path.join(__dirname, 'views'));
/*jslint nomen: false*/
app.set('view engine', 'ejs');
// Routes
var coinkite = require('./routes/coinkite');
app.use('/coinkite', coinkite);
// Start Server
app.listen(currentPort);
console.log('Server is running at port: ' + currentPort);
This is the route for coinkite,
/*global require*/
/*global console*/
/*global module*/
/*global process*/
/*global CK_API */
// Dependancies
var express = require('express');
var crypto = require('crypto');
var request = require('request');
var CK_API_KEY = 'XX-XX-XXXX';
var CK_API_SECRET = 'XXXXXXXXXXX';
var CK_ENDPOINT = '/v1/my/self';
var CK_DATA = '';
var router_CK = express.Router();
router_CK.get('/', function (req, res) {
'use strict';
var ts = (new Date()).toISOString(),
sign = CK_ENDPOINT + '|' + ts,
hm = crypto.createHmac('sha256', CK_API_KEY).update(sign).digest('hex'),
options = { uri: 'http://ift.tt/1FpLsAw' + CK_ENDPOINT, headers: { 'X-CK-Key': CK_API_KEY, 'X-CK-Sign': hm, 'X-CK-Timestamp': ts }, json: true};
request.get(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log(body);
CK_DATA = JSON.parse(body);
console.log(CK_DATA);
}
});
res.render('coinkite-index', {data: CK_DATA});
});
// Return Router
module.exports = router_CK;
On a side note, if I wanted to pass a variable between modules in Node.js how would I do it?
Appreciate the help.
Cheers
Aucun commentaire:
Enregistrer un commentaire