7

Il problema che ho è legato al seguente citazione dal official documentation:Manipolazione database di configurazione dell'ambiente in Sails.js

Note If any connection to an adapter is used by a model, then all connections to that adapter will be loaded on sails.lift, whether or not models are actually using them. In the example above, if a model was configured to use the localMysql connection, then both localMysql and remoteMysql would attempt to connect at run time. It is therefore good practice to split your connection configurations up by environment and save them to the appropriate environment-specific config files, or else comment out any connections that you don't want active.

Come si può configurare la connessione per un server di produzione?

miei connections.js di file è simile al seguente:

module.exports.connections = { 

    mongoDev: { 
    adapter: 'sails-mongo', 
    host: 'localhost', 
    port: 27017, 
    user: 'username', 
    password: 'password', 
    database: 'database' 
    }, 

    mongoLive: { 
    adapter: 'sails-mongo', 
    host: 'host.mongolab.com', 
    port: 31681, 
    user: 'user', 
    password: 'password', 
    database: 'database' 
    } 
}; 

E nei miei file di configurazione dell'ambiente c'ho:

development.js

module.exports = { 
    models: { 
    connection: 'mongoDev' 
    }  
}; 

production.js

module.exports = { 
    models: { 
    connection: 'mongoLive' 
    }, 
    port: 3000, 
}; 

Questo funziona sul computer locale, poiché il server del database di produzione si trova su un server esterno. Sul ambiente di produzione che sto ottenendo il seguente errore:

[Error: failed to connect to [localhost:27017]] 

Funziona se rimuovere l'oggetto mongoDev dal mio l'oggetto connessioni.

Ho anche provato ad usare adaptors.js, ma questo ha provocato solo alcuni errori di deprecazione.

$ sails -v 
info: v0.9.9 

sto ottenendo qualcosa di diverso durante l'esecuzione sails lift:

info: Sails   
info: v0.10.5 

risposta

12

Si vuole salvare la definizione connessione effettiva negli sia development.js o production.js e rimuoverli dal connections.js. È un po 'non intuitivo.

development.js

module.exports = { 
    connections : { 
    mongoDev: { 
     adapter: 'sails-mongo', 
     host: 'localhost', 
     port: 27017, 
     user: 'username', 
     password: 'password', 
     database: 'database' 
    } 
    }, 
    models: { 
    connection: 'mongoDev' 
    }  
}; 

production.js

module.exports = { 
    connections : { 
    mongoLive: { 
     adapter: 'sails-mongo', 
     host: 'host.mongolab.com', 
     port: 31681, 
     user: 'user', 
     password: 'password', 
     database: 'database' 
    } 
    }, 
    models: { 
    connection: 'mongoLive' 
    }, 
    port: 3000, 
}; 
+0

Questo non sembra funzionare per me. Sai perché devi farlo con il setup dell'ambiente? C'è qualcosa che lo costringe a tornare al locale? – Scott

+1

questa risposta ha collegamenti sul motivo per cui questo * era * un problema http://stackoverflow.com/questions/28658299/sailsjs-still-uses-default-database-after-changing-it-to-mongodb/28664845#28664845 – Meeker