如何在Stampery API.JS文件中设置API密钥

时间:2017-06-09 21:00:07

标签: javascript node.js express

我正在设置Stampery。我无法确定在此API.JS文件中设置字符串API密钥的位置。文档说将STAMPERY_TOKEN设置为API密钥,不知道如何执行此操作。任何帮助,将不胜感激。

Stampery的链接是https://github.com/stampery/office

'use strict';

const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser')
const Stampery = require('stampery');

const development = process.env.NODE_ENV !== 'production';
const stamperyToken = process.env.STAMPERY_TOKEN;

var proofsDict = {}

if (!stamperyToken) {
  console.error('Environment variable STAMPERY_TOKEN must be set before running!');
  process.exit(-1);
}

//var stampery = new Stampery(process.env.STAMPERY_TOKEN, development ? 'beta' : false);
// For now, always use production Stampery API due to not making it work against beta.
var stampery = new Stampery(process.env.STAMPERY_TOKEN);

router.use(bodyParser.json());

router.post('/stamp', function (req, res) {
  var hash = req.body.hash;

  // Throw error 400 if no hash
  if (!hash)
    return res.status(400).send({error: 'No Hash Specified'});

  // Transform hash to upper case (Stampery backend preferes them this way)
  hash = hash.toUpperCase()

  // Throw error 422 if hash is malformed
  var re = /^[A-F0-9]{64}$/;
  if (!(re.test(hash)))
    return res.status(422).send({error: 'Malformed Hash'});

  stampery.stamp(hash, function(err, receipt) {
    if (err)
      res.status(503).send({error: err});
    else
      res.send({result: receipt.id, error: null});
  });

});

router.get('/proofs/:hash', function (req, res) {
  var hash = req.params.hash;

  stampery.getByHash(hash, function(err, receipts) {
    if (err)
      res.status(503).send({error: err});
    else
      if (receipts.length > 0)
        res.send({result: receipts[0], error: null});
      else
        res.status(200).send({error: 'Oops! This email has not yet been attested by any blockchain.'});
  });

});

module.exports = router;

我在Azure网站中添加了以下内容。这应该足够了: enter image description here

2 个答案:

答案 0 :(得分:1)

在启动服务器之前,您需要设置STAMPERY_TOKEN环境可用。

您可以这样执行此操作(例如在Windows中)set STAMPERY_TOKEN=your-token&& node app.js

答案 1 :(得分:1)

有两种方法可以将其添加到环境中(对于Ubuntu)。

  1. 添加到bashrc文件。像:

      

    导出STAMPERY_TOKEN ="你的东西"

  2. 在运行服务器之前传递这些参数。像:

      

    STAMPERY_TOKEN =您的TOKEN节点server.js

  3. 要访问此变量,您可以通过以下方式获取:

      

    的console.log(process.env [" STAMPERY_TOKEN"]);