通常您會透過 Sails 的 命令列介面 與之互動,使用 sails lift
啟動伺服器,但 Sails 應用程式也可以透過使用 程式化介面,從其他 Node 應用程式內部啟動和操作。此介面的主要用途之一是在自動化測試套件中執行 Sails 應用程式。
若要從 Node.js 腳本中建立新的 Sails 應用程式,請使用 Sails 的建構函式。相同的建構函式可以用來建立任意多個不同的 Sails 應用程式
var Sails = require('sails').constructor;
var mySailsApp = new Sails();
var myOtherSailsApp = new Sails();
一旦您取得新 Sails 應用程式的參考,您可以使用 .load()
或 .lift()
來啟動它。這兩個方法都接受兩個參數:組態選項的字典,以及在 Sails 應用程式啟動後將執行的回呼函式。
當 Sails 以程式化的方式啟動時,它仍然會使用目前工作目錄下的
api
、config
和其他資料夾來載入控制器、模型和組態選項。一個值得注意的例外是,以這種方式啟動應用程式時,不會載入.sailsrc
檔案。
作為參數傳遞給
.load()
或.lift()
的任何組態選項,其優先順序將高於從其他任何地方載入的選項。
透過環境變數設定的組態選項不會自動應用於以程式化的方式啟動的 Sails 應用程式,但
NODE_ENV
和PORT
除外。
若要從
.sailsrc
檔案和環境變數載入組態選項,請使用 Sails 透過require('sails/accessible/rc')
提供的rc
模組。
.load()
和 .lift()
之間的區別在於,.lift()
採取額外的步驟:(1) 執行應用程式的 bootstrap(如果有的話),以及 (2) 在透過 sails.config.port
設定的端口(預設為 1337)上啟動 HTTP 伺服器。這允許您對啟動的應用程式發出 HTTP 請求。若要對使用 .load()
啟動的應用程式發出請求,您可以使用已載入應用程式的 .request()
方法。
在端口 1338 上使用 .lift()
啟動應用程式,並透過 HTTP 發送 POST 請求
var request = require('request');
var Sails = require('sails').constructor;
var mySailsApp = new Sails();
mySailsApp.lift({
port: 1338
// Optionally pass in any other programmatic config overrides you like here.
}, function(err) {
if (err) {
console.error('Failed to lift app. Details:', err);
return;
}
// --•
// Make a request using the "request" library and display the response.
// Note that you still must have an `api/controllers/FooController.js` file
// under the current working directory, with an `index` action,
// or a `/foo` or `POST /foo` route set up in `config/routes.js`.
request.post('/foo', function (err, response) {
if (err) {
console.log('Could not send HTTP request. Details:', err);
}
else {
console.log('Got response:', response);
}
// >--
// In any case, whether the request worked or not, now we need to call `.lower()`.
mySailsApp.lower(function (err) {
if (err) {
console.log('Could not lower Sails app. Details:',err);
return;
}
// --•
console.log('Successfully lowered Sails app.');
});//</lower sails app>
});//</request.post() :: send http request>
});//</lift sails app>
使用目前環境和 .sailsrc 設定,使用 .lift()
啟動應用程式
var Sails = require('sails').constructor;
var rc = require('sails/accessible/rc');
var mySailsApp = new Sails();
mySailsApp.lift(rc('sails'), function(err) {
});
以下是前一個範例的替代方案:使用 .load()
啟動 Sails 應用程式,並發送語義上相同的 POST 請求,但這次我們將使用虛擬請求而不是 HTTP
mySailsApp.load({
// Optionally pass in any programmatic config overrides you like here.
}, function(err) {
if (err) {
console.error('Failed to load app. Details:', err);
return;
}
// --•
// Make a request using the "request" method and display the response.
// Note that you still must have an `api/controllers/FooController.js` file
// under the current working directory, with an `index` action,
// or a `/foo` or `POST /foo` route set up in `config/routes.js`.
mySailsApp.request({url:'/foo', method: 'post'}, function (err, response) {
if (err) {
console.log('Could not send virtual request. Details:', err);
}
else {
console.log('Got response:', response);
}
// >--
// In any case, whether the request worked or not, now we need to call `.lower()`.
mySailsApp.lower(function (err) {
if (err) {
console.log('Could not lower Sails app. Details:',err);
return;
}
// --•
console.log('Successfully lowered Sails app.');
});//</lower sails app>
});//</send virtual request to sails app>
});//</load sails app (but not lift!)>
若要以程式化的方式停止應用程式,請使用 .lower()
mySailsApp.lower(function(err) {
if (err) {
console.log('An error occured when attempting to stop app:', err);
return;
}
// --•
console.log('Lowered app successfully.');
});
moduleDefinitions
新增動作、模型等警告: 使用
moduleDefinitions
設定宣告式載入模組目前為實驗性功能,並且即使在主要版本發布之間也可能發生重大變更。在使用此設定之前,請確保您專案的 Sails 相依性已釘選到確切的版本(即沒有^
)。
每當 Sails 應用程式啟動時,它通常會載入並初始化儲存在 api/*
中的所有模組(例如來自 api/models
的模型、來自 api/policies
的策略等)。您可以透過在使用 moduleDefinitions
鍵,在作為第一個參數傳遞給 .load()
或 .lift()
的執行階段組態中指定它們,來新增額外的模組。這主要在執行測試時很有用。
以下 Sails 模組可以以程式化的方式新增
模組類型 | 組態鍵 | 詳細資訊 |
---|---|---|
動作 | controllers.moduleDefinitions |
將獨立動作路徑對應到動作定義(經典或 Actions2)的字典。 |
助手 | helpers.moduleDefinitions |
將助手名稱對應到助手定義的字典。 |
模型 | orm.moduleDefinitions.models |
將模型識別碼(小寫模型名稱)對應到模型定義的字典。 |
策略 | policies.moduleDefinitions |
將策略名稱(例如 isAdmin )對應到策略函式的字典。 |
Sails 程式化介面的完整參考可在參考 > 應用程式中找到。