Application class used to create a new instance of an Ash application
via the static method start
Methods
- constructor
- start static
Starts an Ash application
Starts application by performing the following operations
1. runs initializers
Runs initializer classes in app/initializers (if any) in alphabetical order. Inializers can be used to hook into app start up early on and gain access to the express app instance in case you need to perform operations outside the scope of the Ash framework.
Example: adding an initializer
// app/initializers/application.js
const Ash = require('@ash-framework/ash')
module.exports = class Initializer extends Ash.Initializer {
init (app) {
// app is an unmodified express app instance so you can
// do the following.
app.get('/animals/mice', function (req, res) {
res.send('Success')
})
}
}
2. loads middleware
Reads app/middleware.js to determine which middleware to run and in what order. Reads in and runs any middleware specified in app/middleware.js from the app/middleware directory
Example: creating middleware
// app/middleware/access.js
const Ash = require('@ash-framework/ash')
module.exports = class AccessMiddleware extends Ash.Middleware {
register () {
}
}
Example: registering middleware
// app/middleware.js
const Ash = require('@ash-framework/ash')
class MiddlewareRouter extends Ash.MiddlewareRouter { }
MiddlewareRouter.map(function () {
this.middleware('access')
})
module.exports = MiddlewareRouter
3. creates routes
Reads app/router.js to determine which routes to register. Reads in and registers any routes specified in app/router.js from the app/routes directory
Example: creating a route
// app/routes/user.js
const Ash = require('@ash-framework/ash')
module.exports = class UserRoute extends Ash.Route {
model () {
// return user data
}
}
Example: registering route in the router
// app/router.js
const Ash = require('@ash-framework/ash')
class Router extends Ash.Router { }
Router.map(function () {
this.route('user')
})
module.exports = Router
4. adds an error handler
This error handler catches any errors and specifies how errors should be handled and displayed to the client.
Looks for a user defined error handler class for the application in app/error-handler.js If not found one is defined in its place.
Example: adding a custom error handler
// app/error-handler.js
const {ErrorHandler, log} = require('@ash-framework/ash')
module.exports = class ApplicationErrorHandler extends ErrorHandler {
error (error) {
log.error(error)
super.error(error)
}
}
5. starts the app
Starts the app on the port described in config/environment.js
. The default port is 3010.
Example: starting an application:
const Ash = require('@ash-framework/ash')
class Application extends Ash.Application {
}
Application.start()