试图访问.yml文件

时间:2017-12-29 21:18:29

标签: node.js

我想要访问我的.yml文件,我在其中通过我的节点应用程序设置了一些应用程序设置。

但是当我尝试运行时出现以下错误

module.js:538
    throw err;
    ^

Error: Cannot find module 'config'
    at Function.Module._resolveFilename (module.js:536:15)
    at Function.Module._load (module.js:466:25)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/lucas/Documents/cash-flow-sdk/src/config/conductor.js:2:16)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)

结构

/config
  default.yml
/node_modules
/src
  /config
    conductor.js

我已经尝试了../../config,但仍然是错误

代码

const request = require('request')
const config = require('config')

const conductorAPIService = {
  get,
  post,
  put,
  remove
}

function get (path) {
  return req(path, 'GET', null)
}

function post (path, data) {
  return req(path, 'POST', data)
}

function put (path, data) {
  return req(path, 'PUT', data)
}

function remove (path) {
  return req(path, 'DELETE', null)
}

function req (path, method, data) {
  const conductorAPI = `${config.get('CONDUCTOR.URL')}${path}`
  return new Promise((resolve, reject) => {
    request({
      method,
      url: conductorAPI,
      json: true,
      body: data,
      headers: {
        client_id: `${config.get('CONDUCTOR.HEADER.CLIENT_ID')}`,
        access_token: `${config.get('CONDUCTOR.HEADER.ACCESS_TOKEN')}`,
        accept: `${config.get('CONDUCTOR.HEADER.ACCEPT')}`,
        'Content-Type': `${config.get('CONDUCTOR.HEADER.CONTENT_TYPE')}`
      }
    }, (err, statusCode, body) => {
      if (err) {
        return reject(err)
      }
      return resolve(body)
    })
  })
}

module.exports = function factory () {
  return conductorAPIService
}

我的Default.yml文件

CONDUCTOR:
  URL: 'some http'
  HEADER:
    CLIENT_ID: 'test'
    ACCESS_TOKEN: 'test'
    ACCEPT: 'application/json'
    CONTENT_TYPE: 'application/json'
  ENDPOINTS:
    ACCOUNT: 'test'
    FINANCIAL_ADJUSTMENT: 'test'
    TRANSFER: 'test'
APP:
  LOG_LEVEL: 'silly'
LOGGER:
  FILE: 'conductor-financial-adjustment-service.log'
  PATH: '../log'

0 个答案:

没有答案