wercker for sails + mongo db

时间:2015-10-01 22:41:45

标签: mongodb sails.js wercker

我尝试使用wercker, 但我不知道我的测试无法连接到我的mongodb。

我使用sails + sails mongo,当npm测试...我总是得到错误可以连接到mongo db,这是我的wercker.yml:

box: nodesource/trusty:0.12.7
services:
  - id: mongo:2.6
# Build definition
build:
  # The steps that will be executed on build
  steps:
    - script:
        name: set NODE_ENV
        code: export NODE_ENV=development
    # A step that executes `npm install` command
    - npm-install
    # A step that executes `npm test` command
    - npm-test
    # A custom script step, name value is used in the UI
    # and the code value contains the command that get executed
    - script:
        name: echo nodejs information
        code: |
          echo "node version $(node -v) running"
          echo "npm version $(npm -v) running"

这是我的错误消息:

warn: `sails.config.express` is deprecated; use `sails.config.http` instead.
Express midleware for passport
error: A hook (`orm`) failed to load!
  1) "before all" hook
  2) "after all" hook

  0 passing (2s)
  2 failing

  1)  "before all" hook:
     Uncaught Error: Failed to connect to MongoDB.  Are you sure your configured Mongo instance is running?
 Error details:
{ [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }
      at net.js:459:14

  2)  "after all" hook:
     Uncaught Error: Failed to connect to MongoDB.  Are you sure your configured Mongo instance is running?
 Error details:
{ [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }
      at net.js:459:14

1 个答案:

答案 0 :(得分:1)

开箱即用时,MongoDB没有身份验证,因此您只需提供正确的主机和端口。

  1. config/connection.js中的sails应用中定义新连接:
  2.     mongodbTestingServer: {
            adapter: 'sails-mongo',
            host:    process.env.MONGO_PORT_27017_TCP_ADDR,
            port:    process.env.MONGO_PORT_27017_TCP_PORT
        },
    

    关于MONGO_PORT_27017_TCP_ADDRMONGO_PORT_27017_TCP_PORT,这两个环境变量是由Wercker在您声明mongo服务时创建的。 就像那样,您将能够使用正确的主机和端口将您的应用程序连接到您的数据库

    1. config/env/testing.js的风帆应用中添加新环境。它将由Wercker使用:
    2. module.exports = {
          models: {
              connection: 'mongodbTestingServer'
          }
      };
      
      1. 在您的wercker文件wercker.yml中。我建议您使用 ewok堆栈(基于Docker),您可以在应用程序的设置中激活它。以下是有关migration to Ewok stack的一些有用信息。我的示例使用基于Docker镜像的框。
      2. # use the latest official stable node image hosted on DockerHub 
        box: node
        # use the mongo (v2.6) image hosted on DockerHub 
        services:
          - id: mongo:2.6
        # Build definition
        build:
            steps:
            # Print node and npm version
            - script:
                name: echo nodejs information
                code: |
                    echo "node version $(node -v) running"
                    echo "npm version $(npm -v) running"
            - script:
                name: set NODE_ENV
                code: |
                    export NODE_ENV=testing
            # install npm dependencies of your project
            - npm-install
            # run tests
            - npm-test
        

        要查看Wercker构建中的所有环境变量,请添加以下行:

        - script:
            name: show all environment variables
            code: |
                env
        

        它应该有用。