Command line interface.
// Add another path to load commands from
app.cli.commandPaths.push(app.home.child('cli').toString());
Application config.
// Remove value
delete app.config.foo;
// Assign multiple values at once
Object.assign(app.config, {foo: 'test', bar: 23});
Default stash values.
// Remove value
delete app.defaults.foo;
// Assign multiple values at once
Object.assign(app.defaults, {foo: 'test', bar: 23});
Detect if the application has been imported and disable the command line interface if it has.
Format for HTTP exceptions ("html", "json", or "txt").
// Change default exception format for whole application
app.exceptionFormat = 'json';
Application home directory.
// Portably generate path relative to home directory
const path = app.home.child('data', 'important.txt');
Application hooks.
// Run a custom hook
await app.hooks.runHook('my:hook', foo, bar);
Application logger.
// Log debug message
app.log.debug('It works');
MIME types.
// Get MIME type for extension
const type = app.mime.extType('txt');
Operating mode for application. Defaults to the value of the NODE_ENV
environment variable or development
.
Storage for user defined models.
// Store database connection
app.models.pg = new Pg('postgres://127.0.0.1:5432/db');
Application renderer.
// Disable compression
app.renderer.autoCompress = false;
// Add another "views" directory
app.renderer.viewPaths.push(app.home.child('views').toString());
Application router.
// Add routes
const r = app.router;
r.get('/foo/bar').to('test#foo', {title: 'Hello Mojo!'});
r.post('/baz').to('test#baz');
// Add another path to load controllers from
app.router.controllerPaths.push(app.home.child('more-controllers').toString());
Rotating secret passphrases used for signed cookies and the like.
// Rotate passphrases
app.secrets = ['new_passw0rd', 'old_passw0rd', 'very_old_passw0rd'];
Encrypted cookie based session manager.
// Change name of cookie used for all sessions
app.sessions.cookieName = 'mysession';
// Disable SameSite feature
app.sessions.sameSite = 'none';
Static file server.
// Add another "public" directory
app.static.publicPaths.push('/home/sri/public');
// Add another "public" directory with higher precedence
app.static.publicPaths.unshift('/home/sri/themes/blue/public');
HTTP/WebSocket user-agent.
# Perform HTTP request
const res = await app.ua.get('http://example.com');
JSON schema validator.
// Add a named schema for later use
app.validator.addSchema({type: 'object', properties: {test: {type: 'number'}}}, 'testForm');
Add a helper.
// Render response with header
app.addHelper('renderWithHeader', async (ctx, ...args) => {
ctx.res.set('X-Mojo', 'I <3 mojo.js!');
await ctx.render(...args);
});
// Render response with header using nested helper
app.addHelper('renderWith.header', async (ctx, ...args) => {
ctx.res.set('X-Mojo', 'I <3 mojo.js!');
await ctx.render(...args);
});
Generate route matching any of the listed HTTP request methods or all.
Rest
...args: AnyArguments// Route with pattern and destination
app.any('/user').to('User#whatever');
// Route with HTTP methods, pattern, restrictive placeholders and destination
app.any(['DELETE', 'PUT'], '/:foo', {foo: /\w+/}).to('Foo#bar');
// Route with pattern, name and destination
app.any('/:foo').name('foo_route').to('Foo#bar');
// Route with pattern, condition and destination
app.any('/').requires({agent: /Firefox/}).to('Foo#bar');
// Route with pattern and a closure as destination
app.any('/:foo', async ctx => ctx.render({text: 'Hello World!'}));
Generate route matching only DELETE
requests.
Rest
...args: RouteArguments// Route with destination
app.delete('/user').to('User#remove');
Generate route matching only GET
requests.
Rest
...args: RouteArguments// Route with destination
app.get('/user').to('User#show');
Handle a new incoming request, used by servers.
Create a context for application.
Create a mock context for application. Very useful for testing helpers.
Optional
headers?: string[]Optional
method?: stringOptional
url?: string// Use a mock context to call a helper
const ctx = app.newMockContext();
const html = ctx.assetTag('/app.js');
Create a new mock user-agent for application.
Optional
options: UserAgentOptionsOptional
serverOptions: ServerOptionsCreate a new test user-agent for application.
Optional
options: TestUserAgentOptionsOptional
serverOptions: ServerOptions// Test plain text endpoint
const ua = await app.newTestUserAgent();
(await ua.getOk('/')).statusIs(200).bodyIs('Hello World!');
Your main hook into the application, it is a shortcut for the app:start
hook and runs during application
startup. You can use it to perform tasks like preparing database connections.
// Perform async operations on application startup
app.onStart(async app => {
if (app.models.db === undefined) app.models.db = new SomeDatabase();
await app.models.db.connect();
});
Generate route matching only OPTIONS
requests.
Rest
...args: RouteArguments// Route with destination
app.options('/user').to('User#overview');
Generate route matching only PATCH
requests.
Rest
...args: RouteArguments// Route with destination
app.patch('/user').to('User#update');
Register plugin.
// Mount application under "/prefix"
app.plugin(mountPlugin, {app: myOtherApp, path: '/prefix'});
// Load configuration from file
app.plugin(jsonConfigPlugin, {file: 'myapp.conf'});
Generate route matching only POST
requests.
Rest
...args: RouteArguments// Route with destination
app.post('/user').to('User#create');
Generate route matching only PUT
requests.
Rest
...args: RouteArguments// Route with destination
app.put('/user').to('User#replace');
Generate route for a nested route with its own intermediate destination.
Rest
...args: AnyArguments// Intermediate destination and prefix shared between two routes
const auth = app.under('/user').to('User#auth');
auth.get('/show').to('User#show');
auth.post('/create').to('User#create');
Generate route matching only WebSocket handshake requests.
Rest
...args: RouteArguments// Route with destination
app.websocket('/echo').to('Example#echo');
Generated using TypeDoc
Application class.