在 Node/Express App 中设置 JSForce Connection 中间件的最佳方式
Best way to set up JSForce Connection middleware in Node/Express App
我有一个简单的节点应用程序,它使用 JSForce 将 Salesforce 连接到 push/query 我们的 Salesforce 帐户。我使用的是 username/password 身份验证而不是 OAuth。
它现在工作正常,但它仍在开发中,我注意到我的页面加载非常慢,可能是因为在每次加载时我都在建立连接、登录并将连接附加到 req 通过中间件。
router.use('/*time*',
sftools.conn, // Create connection to Salesforce
sftools.login, // Login to SalesForce
sftools.picklist //populate res.locals.roles with SalesForce Roles
)
中间件代码示例:
const jsforce = require('jsforce')
exports.conn = (req, res, next) => {
req.conn = new jsforce.Connection({
loginUrl: process.env.SF_LOGINURL
})
next()
}
exports.login = (req, res, next) => {
req.conn.login(process.env.SF_USER, process.env.SF_PASS, (err) => {
if(err) return next(err)
next()
})
}
再次,这有效,但我认为我应该设置连接并在全局登录,我只是不确定具体如何执行此操作以及它应该在我的应用程序中的什么位置。即使我这样做了,我还是应该将 conn 分配给请求,还是直接访问它?第一个节点应用程序...任何想法都将受到赞赏。
如果您希望连接在请求已处理后仍保持打开状态,则应将连接处理提取到专用模块/文件中。
从节点内调用 'require' 将始终 return 相同的实例:
(...) every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.
Multiple calls to require('foo') may not cause the module code to be executed multiple times. (...)
Please check the nodejs documentation for further information:
考虑到这一点,您可以创建一个专用的处理程序文件...
// sfConnectionHandler.js
const jsforce = require('jsforce')
let currentConnection
function createConnection() {
currentConnection = new jsforce.Connection({ loginUrl: process.env.SF_LOGINURL })
currentConnection.login(process.env.SF_USER, process.env.SF_PASS, (err) => {
// your error handling
})
// use promises, call next(), ...
}
function connection() {
// assert whatever you feel is necessary (is alive, logged in, ...)
if(!currentConnection) {
createConnection();
}
return currentConnection;
}
module.exports = connection;
...然后在您要使用连接的任何地方需要此文件。
const sfConnectionHandler = require('./sfConnectionHandler');
sfConnectionHandler.connection.query('...');
连接不能附加到每个请求。我想不出有什么优点,只要没有特殊的联系就行。
我有一个简单的节点应用程序,它使用 JSForce 将 Salesforce 连接到 push/query 我们的 Salesforce 帐户。我使用的是 username/password 身份验证而不是 OAuth。
它现在工作正常,但它仍在开发中,我注意到我的页面加载非常慢,可能是因为在每次加载时我都在建立连接、登录并将连接附加到 req 通过中间件。
router.use('/*time*',
sftools.conn, // Create connection to Salesforce
sftools.login, // Login to SalesForce
sftools.picklist //populate res.locals.roles with SalesForce Roles
)
中间件代码示例:
const jsforce = require('jsforce')
exports.conn = (req, res, next) => {
req.conn = new jsforce.Connection({
loginUrl: process.env.SF_LOGINURL
})
next()
}
exports.login = (req, res, next) => {
req.conn.login(process.env.SF_USER, process.env.SF_PASS, (err) => {
if(err) return next(err)
next()
})
}
再次,这有效,但我认为我应该设置连接并在全局登录,我只是不确定具体如何执行此操作以及它应该在我的应用程序中的什么位置。即使我这样做了,我还是应该将 conn 分配给请求,还是直接访问它?第一个节点应用程序...任何想法都将受到赞赏。
如果您希望连接在请求已处理后仍保持打开状态,则应将连接处理提取到专用模块/文件中。
从节点内调用 'require' 将始终 return 相同的实例:
(...) every call to require('foo') will get exactly the same object returned, if it would resolve to the same file. Multiple calls to require('foo') may not cause the module code to be executed multiple times. (...)
Please check the nodejs documentation for further information:
考虑到这一点,您可以创建一个专用的处理程序文件...
// sfConnectionHandler.js
const jsforce = require('jsforce')
let currentConnection
function createConnection() {
currentConnection = new jsforce.Connection({ loginUrl: process.env.SF_LOGINURL })
currentConnection.login(process.env.SF_USER, process.env.SF_PASS, (err) => {
// your error handling
})
// use promises, call next(), ...
}
function connection() {
// assert whatever you feel is necessary (is alive, logged in, ...)
if(!currentConnection) {
createConnection();
}
return currentConnection;
}
module.exports = connection;
...然后在您要使用连接的任何地方需要此文件。
const sfConnectionHandler = require('./sfConnectionHandler');
sfConnectionHandler.connection.query('...');
连接不能附加到每个请求。我想不出有什么优点,只要没有特殊的联系就行。