profoundjs.express()
This API creates an Express callback from a Profound.js module which will receive the Express Request and Response objects as parameters. The returned function can be used with app.all(), app.get(), and similar Express APIs. If the configuration value connectorCredentials is set, this API can automatically sign on to IBM i. See Connecting to a Remote Instance. If the instance is running on IBM i, a Profound UI session id can be passed on the request as a parameter named AUTH. If present, the API will automatically set the current user profile and library list of the connection to match the Profound UI session.
Parameters
module (string or function):
If a string is passed, it should contain the path to or name of the Profound.js module to be wrapped. The function exported as "run" in the wrapped Profound.js module will be called.Â
In order to pass a function, profoundjs.require() must first be used to load a module. Â
authorize (string or function, optional)Â
If a string is passed, it should contain the path to or name of the Profound.js authenticate and authorize module. The function exported as "run" in the wrapped Profound.js module will be called.Â
In order to pass a function, profoundjs.require() must first be used to load a module.Â
Examples
In start.js
var isWorker = profoundjs.server.listen();
if (isWorker) {
var app = profoundjs.server.app;
app.all("/products/:id", profoundjs.express("products.js"));
app.all("/products/:id", profoundjs.express("products.js", "apiAuthentication")); // With authentication
}
products.js
function products(req, res) {
pjs.defineTable("productsp", { keyed: true, read: true });
productsp.getRecord(Number(req.params.id));
if (productsp.found()) {
res.json({
prid: prid,
prname: prname.trim(),
prdesc: prdesc.trim(),
prprice: prprice,
primage: primage.trim(),
prqty: prqty,
prcatid: prcatid
});
}
else {
res.sendStatus(404);
}
}
module.exports.run = products;
Example of apiAuthentication.js
function authenticate(request, response, next) {
const Fiber = require('profoundjs-fibers');
// Do what is needed to authenticate/authorize the user:: pjs.query, pjs.call, ...
// For example, say a request contains an API key in a header
// And you have a table that contains API keys and values for API Users
// This authentication logic might look something like this:
let filter = pjs.data.createCondition("apiKey", "=", request.headers["api-key"]);
let user = pjs.data.get("apiUsers", filter, 1);
if (!user)
throw new pjs.NotAuthorizedError();
Fiber.current.session.user = user.name;
// "next() -- is what will call your webservice
next();
}
exports.run = authenticate;