3.4.0 • Published 8 months ago
@epiijs/server v3.4.0
@epiijs/server
A simple server framework.
- functional pipeline
- file-system based routor
- handler-in-action filter
- service dependency injection
v3.x is only for ES module.
Install
npm i @epiijs/server --saveUsage
project like this
(root)
├─ src
│ ├─ actions
│ │ ├─ $params
│ │ │ └─ index.ts
│ │ └─ index.ts
│ └─ services
│ └─ service.ts
└─ start.tswill routes requests like this
=> /$params
=> /start server
import { startServer } from '@epiijs/server';
startServer({
name: 'your-server',
port: 8080,
path: {
root: __dirname // or getDirNameByImportMeta(import.meta)
}
});handle request by action
Provide request handlers in /actions.
import {
ActionResult,
IncomingMessage
} from '@epiijs/server';
export default async function (props: IncomingMessage): Promise<ActionResult> {
const { method, url } = props;
// simple response
return 'hello world';
// custom response
return {
status: 400,
headers: { 'content-type': 'application/json' },
content: JSON.stringify({})
};
// or you can throw error
throw new Error('fatal error');
}filter pipeline by handler in action
Use useHandler to filter request and dispose after action called.
import {
ActionResult,
Context,
IncomingMessage
} from '@epiijs/server';
export default async function (props: IncomingMessage, context: Context): Promise<ActionResult> {
const { method, url } = props;
await context.useHandler(dispose => {
const start = Date.now();
if (method !== 'GET') {
return { status: 405, content: 'method not allowed' };
}
dispose(() => {
console.log('elapsed', Date.now() - start);
});
});
return 'hello world';
}inject service as dependency
Provide service factory in /services.
export interface IUserService {}
export default function createUserService() {
const userService: IUserService = {};
return userService;
}Use useService in action to get service instance.
import {
ActionResult,
Context,
IncomingMessage
} from '@epiijs/server';
export default async function (props: IncomingMessage, context: Context): Promise<ActionResult> {
const { method, url } = props;
const userService = await context.useService('UserService');
const users = await userService.findUsers();
return users;
}Document
WIP
- global error handling
- declare action and service
3.4.0
8 months ago
3.3.2
2 years ago
3.3.1
2 years ago
3.3.0
2 years ago
3.2.0
2 years ago
3.1.0
2 years ago
3.0.0
2 years ago
2.1.0
2 years ago
2.0.0
2 years ago
1.1.2
4 years ago
1.1.1
5 years ago
1.1.0
5 years ago
1.0.0
5 years ago
0.13.0
5 years ago
0.12.1
5 years ago
0.12.0
5 years ago
0.11.1
5 years ago
0.11.0
5 years ago
0.10.3
5 years ago
0.10.2
5 years ago
0.10.1
5 years ago
0.10.0
5 years ago
0.9.0
5 years ago
0.8.0
6 years ago
0.7.0
6 years ago
0.6.2
6 years ago
0.6.1
6 years ago
0.5.3
6 years ago
0.6.0
6 years ago
0.5.2
7 years ago
0.5.1
7 years ago
0.5.0
7 years ago