3.3.2 • Published 3 months ago

@epiijs/server v3.3.2

Weekly downloads
37
License
MIT
Repository
github
Last release
3 months ago

@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 --save

Usage

project like this

(root)
├─ src
│  ├─ actions
│  │  ├─ $params
│  │  │  └─ index.ts
│  │  └─ index.ts
│  └─ services
│     └─ service.ts
└─ start.ts

will 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.3.2

3 months ago

3.3.1

3 months ago

3.3.0

4 months ago

3.2.0

4 months ago

3.1.0

4 months ago

3.0.0

4 months ago

2.1.0

7 months ago

2.0.0

7 months ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago

0.13.0

3 years ago

0.12.1

3 years ago

0.12.0

3 years ago

0.11.1

3 years ago

0.11.0

3 years ago

0.10.3

4 years ago

0.10.2

4 years ago

0.10.1

4 years ago

0.10.0

4 years ago

0.9.0

4 years ago

0.8.0

4 years ago

0.7.0

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.5.3

4 years ago

0.6.0

4 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago