harno-kernel-server v0.0.1
Harno-kernel-server
Npm module that helps you to build your node js server-like projects.
This module is basically the same as harno-kernel
, but it also contains a lot of usefull services for node
Install
npm install harno-kernel-server
Usage
Basic
main.js
// you can export ServerKernel instance
const { YourService } = require('./your.file.js')
const serverKernel = require('harno-kernel-server').default
export default class App {
constructor () {
// you can init all default services and it's dependencies
serverKernel.registerDefaultServices()
serverKernel.initDefaultServicesDependencies()
// some services have logger service as their dependencies, so before initing them, it would be great to configure logger
serverKernel.getService('logger').config({ ... })
// call init methods of some services
serverKernel.initDefaultServices()
// you can register your services like this
serverKernel.registerService('yourAnyName', YourService)
// you can init dependencies for your service like this
serverKernel.initDependencies({
yourAnyName: ['express', 'config']
})
// you can get any service by its name like this
const yourAnyName = serverKernel.getService('yourAnyName')
// or
const yourAnyName = serverKernel.s.yourAnyName
}
}
Advanced
main.js
// you can export ServerKernel class, or any other available service
const { YourService } = require('./your.file.js')
const { ServerKernel, ConfigService, ExpressService, ... } = require('harno-kernel-server').default
export default class App {
constructor () {
this.kernel = new ServerKernel()
// you can register services
this.kernel.registerService('express', ExpressService)
this.kernel.registerService('config', ConfigService)
this.kernel.registerService('yourAnyName', YourService)
// you can init dependencies for any service (for some default services you must do that)
this.kernel.initDependencies({
yourAnyName: ['express', 'config']
})
// you need to call init function of some default services
this.kernel.s.express.init()
// in this example YourService would have property _dependencies or _d with links to express and config services
const yourAnyName = this.kernel.getService('yourAnyName')
// look at the your.file.js bellow
console.log(yourAnyName.afterInitDependencies())
// output: My dependencies is: express and config
}
async start () {
// you can get services like this
this.kernel.getService('configService').readJson('./path.to.json')
// or like this
const port = this.kernel.s.configService.c.port
await this.kernel.s.expressService.start(port)
}
}
your.file.js
class YourService {
afterInitDependencies () {
// after init your service will have property _dependencies or _d, that will contain all inited dependencies
const express = this._d.express
const config = this._d.config
if (express && config) {
console.log('My dependencies is: express and config')
}
}
}
export { YourService }
API
new ServerKernel()
Creates new server kernel instance
serverKernel.initDependencies(dependenciesConfig)
- dependenciesConfig example:
const dependenciesConfig = {
serviceName1: ['serviceName2', 'serviceName3'],
serviceName2: ['serviceName4'],
...
}
Inits/sets services dependencies, so that in our example service serviceName1
will have property _dependencies
or _d
that would contain serviceName2
, serviceName3
services
serverKernel.registerService(name, Service)
- name - service name (can be any string)
- Service - any js class
Register/saves Service
class instance under the name
in the server kernel
serverKernel.getService(name)
- name - service name
Gets service that was created under that name
serverKernel.s
Kernel property that contains all registered services
serverKernel.registerDefaultServices()
Registers all default services under default names (look at Defaut services
for more info)
serverKernel.initDefaultServicesDependencies()
Inits all default services dependencies
serverKernel.initDefaultServices()
Inits all default services. Calls init
method of default services
Default services
JobsSchedulerService
- Default name:
jobsScheduler
- Dependencies:
['logger']
- Description: Class that manages jobs (jobs are functions that behave like cron jobs)
- API: Work in progress, so more reliable api would be announced later
ExpressService
- Default name:
express
- Dependencies:
['logger']
- Description: Class that manages express
- API: Work in progress, so more reliable api would be announced later
ConfigService
- Default name:
config
- Dependencies:
none
- Description: Class that manages configuration
- API: Work in progress, so more reliable api would be announced later
MongoDBService
- Default name:
mongoDB
- Dependencies:
['logger']
- Description: Class that manages mongo database with mongoose npm module
- API: Work in progress, so more reliable api would be announced later
LoggerService
- Default name:
logger
- Dependencies:
none
- Description: Class that helps with logging
- API: Work in progress, so more reliable api would be announced later
ErrorHandlerService
- Default name:
errorHandler
- Dependencies:
['logger']
- Description: Class that handles different errors
- API: Work in progress, so more reliable api would be announced later
RequestService
- Default name:
request
- Dependencies:
none
- Description: Class that does different network requests
- API: Work in progress, so more reliable api would be announced later
TickService
- Default name:
tick
- Dependencies:
['logger']
- Description: Class that manages tick functions (functions that is executed per interval)
- API: Work in progress, so more reliable api would be announced later
SwaggerService
- Default name:
swagger
- Dependencies:
['express']
- Description: Class that manages swagger documentation
- API: Work in progress, so more reliable api would be announced later
AuthRequestsService
- Default name:
authRequests
- Dependencies:
['request']
- Description: Class that helps with authorization routes for (harno-auth mostly at this point)
- API: Work in progress, so more reliable api would be announced later
ValidationService
- Default name:
validation
- Dependencies:
['errorHandler']
- Description: Class that validates different values
- API: Work in progress, so more reliable api would be announced later
License
5 years ago