The Feathers application is the main entry point for a Feathers server. It extends Node’s EventEmitter and provides methods for registering services, configuring the application, and managing the application lifecycle.
feathers()
Creates a new Feathers application instance.
import { feathers } from '@feathersjs/feathers'
const app = feathers<Services, Settings>()
Type definition for all services registered on the application
Type definition for application settings
app
Application<Services, Settings>
A new Feathers application instance
Properties
version
The Feathers version number.
import { feathers } from '@feathersjs/feathers'
const app = feathers()
console.log(app.version) // e.g., "5.0.0"
services
An object containing all registered services keyed by their path.
Services should always be retrieved via app.service('name') not via app.services. Direct access to app.services is not recommended.
settings
An object containing all application settings that can be accessed via app.get() and app.set().
mixins
app.mixins: ServiceMixin<Application<Services, Settings>>[]
A list of callbacks that run when a new service is registered. Used internally for adding hooks and events functionality.
app.mixins.push((service, path, options) => {
console.log(`Registered service at ${path}`)
})
Methods
get()
Retrieve an application setting by name.
get<L extends keyof Settings & string>(name: L): Settings[L]
The setting value, or undefined if not set
app.set('port', 3030)
const port = app.get('port') // 3030
set()
Set an application setting.
set<L extends keyof Settings & string>(name: L, value: Settings[L]): this
The application instance for chaining
app.set('port', 3030)
.set('host', 'localhost')
.set('env', 'development')
Runs a callback configure function with the current application instance.
configure(callback: (this: this, app: this) => void): this
The callback (app: Application) => void to run. The callback is called with this bound to the app instance.
The application instance for chaining
import { feathers } from '@feathersjs/feathers'
const configureApp = (app) => {
app.set('port', 3030)
app.set('host', 'localhost')
}
const app = feathers()
.configure(configureApp)
use()
Register a new service or a sub-app. When passed another Feathers application, all its services will be re-registered with the path prefix.
use<L extends keyof Services & string>(
path: L,
service: ServiceInterface | Application,
options?: ServiceOptions
): this
The path for the service to register (e.g., '/users', '/api')
service
ServiceInterface | Application
required
The service object to register or another Feathers application to use as a sub-app
Configuration options for this service
A list of service methods that should be available externally to clients
A list of custom events that this service emits to clients
Provide a full list of events that this service should emit. Unlike events, this will not be merged with the default events.
The application instance for chaining
// Register a service
app.use('/users', {
async find(params) {
return []
},
async get(id, params) {
return { id }
}
})
// Register with options
app.use('/messages', messageService, {
methods: ['find', 'get', 'create'],
events: ['typing']
})
// Register a sub-app
const subApp = feathers()
subApp.use('/service1', service1)
app.use('/api', subApp) // Mounts at /api/service1
service()
Get the Feathers service instance for a path. This will be the service originally registered with Feathers functionality like hooks and events added.
service<L extends keyof Services & string>(
path: L
): FeathersService<this, Services[L]>
The name of the service (e.g., 'users', '/api/messages')
The service instance with hooks and events
const userService = app.service('users')
const user = await userService.get(1)
unuse()
Unregister an existing service. Calls teardown on the service if it exists.
unuse<L extends keyof Services & string>(
path: L
): Promise<FeathersService<this, Services[L]>>
The name of the service to unregister
The unregistered service instance
const removedService = await app.unuse('users')
setup()
Set up the application and call all services’ .setup() method if available.
setup(server?: any): Promise<this>
A server instance (e.g., HTTP server)
import { feathers } from '@feathersjs/feathers'
import { createServer } from 'http'
const app = feathers()
app.use('/users', userService)
const server = createServer(app)
await app.setup(server)
server.listen(3030)
teardown()
Tear down the application and call all services’ .teardown() method if available.
teardown(server?: any): Promise<this>
A server instance (e.g., HTTP server)
// Graceful shutdown
process.on('SIGTERM', async () => {
await app.teardown()
process.exit(0)
})
hooks()
Register application-level hooks that run for all services.
hooks(map: ApplicationHookOptions<this>): this
map
ApplicationHookOptions
required
The application hook settings
The application instance for chaining
See the Hooks API for details on hook types and usage.
// Register hooks for all services
app.hooks({
before: {
all: [authenticate('jwt')]
},
error: {
all: [logError]
}
})
// Register setup/teardown hooks
app.hooks({
setup: [initializeDatabase],
teardown: [closeConnections]
})
defaultService()
Returns a fallback service instance that will be registered when no service was found. Usually throws a NotFound error but also used to instantiate client-side services.
defaultService(location: string): ServiceInterface
A fallback service instance
// Override default behavior
app.defaultService = (location) => {
return {
async get(id) {
return { id, location }
}
}
}
Events
The Feathers application extends Node’s EventEmitter, so you can use all EventEmitter methods:
// Listen for events
app.on('event-name', (data) => {
console.log('Event received:', data)
})
// Emit events
app.emit('event-name', { some: 'data' })
// Remove listener
app.removeListener('event-name', listener)
Type Definitions
interface Application<Services = any, Settings = any>
extends FeathersApplication<Services, Settings>, EventEmitter {
version: string
services: Services
settings: Settings
mixins: ServiceMixin<Application<Services, Settings>>[]
_isSetup: boolean
get<L extends keyof Settings & string>(name: L): Settings[L]
set<L extends keyof Settings & string>(name: L, value: Settings[L]): this
configure(callback: (this: this, app: this) => void): this
use<L extends keyof Services & string>(
path: L,
service: ServiceInterface | Application,
options?: ServiceOptions
): this
service<L extends keyof Services & string>(path: L): FeathersService<this, Services[L]>
unuse<L extends keyof Services & string>(path: L): Promise<FeathersService<this, Services[L]>>
setup(server?: any): Promise<this>
teardown(server?: any): Promise<this>
hooks(map: ApplicationHookOptions<this>): this
defaultService(location: string): ServiceInterface
}