项目作者: fuse-box

项目描述 :
A lightweight HTTP server implementation based on express with simplicity in mind.
高级语言: TypeScript
项目地址: git://github.com/fuse-box/fuse-http.git
创建时间: 2017-11-29T12:09:15Z
项目社区:https://github.com/fuse-box/fuse-http

开源协议:

下载


FuseHTTP

FuseHTTP is a lightweight HTTP server implementation based on express with simplicity in mind.

  1. import { Route } from 'fuse-http';
  2. @Route("/")
  3. export class MainRoute {
  4. public async get(req) {
  5. return "hello world";
  6. }
  7. }

Key features

  • Simple setup
  • Decorator based
  • Automatic dependency injections by name
  • Custom error handling
  1. npm install fuse-http --save

Getting started

You would need fuse.js configuration to bundle and start your project. So let’s create fuse.js first

fuse.js

  1. const { FuseBox } = require("fuse-box");
  2. const fuse = FuseBox.init({
  3. homeDir: "src",
  4. target: "server@esnext",
  5. output: "dist/$name.js"
  6. });
  7. fuse.bundle("app")
  8. .instructions(">[index.ts]")
  9. .watch().completed(proc => proc.start())
  10. fuse.run();

Now let’s create our application entry point

index.ts

  1. import { Server } from "fuse-http";
  2. // just import the route module here
  3. import './routes/MainRoute';
  4. // start the server
  5. Server.start({ port: 3001 });

Sample route: routes/MainRoute

  1. import { Route } from 'fuse-http';
  2. @Route("/")
  3. export class MainRoute {
  4. public get(req) {
  5. return "hello world";
  6. }
  7. }

Routing

  1. import { Route } from 'fuse-http';
  2. @Route("/")
  3. export class MainRoute {
  4. public async get(req) {
  5. return "hello world";
  6. }
  7. public async post() {
  8. return {};
  9. }
  10. public async anyOtherHTTPMethod() {
  11. return "any other";
  12. }
  13. }

HTTPMethod name must be found in the Route class, once matched it will be executed (with dependency injections).

You can return any kind of object, it will be serialised to JSON if possible. (strings for example will be printed as is)

Dependency injections

The framework comes with default dependency injections to make your life easier.

Name Object
req Express.req
res Express.res
next Express.next

THe order should not matter, the only thing what matter is naming. For example the following example should work just fine.

  1. @Route("/")
  2. export class MainRoute {
  3. public async get(next: express.NextFunction) {
  4. return "hello world";
  5. }
  6. }

Creating your first injection

The following module must be imported in your entry point.

  1. import { Injector } from "fuse-http";
  2. @Injector("foo")
  3. class Foo {
  4. private inject(req : express.Request) {
  5. return this;
  6. }
  7. public helloWorld(){
  8. return { hello : "world" }
  9. }
  10. }

Now as we registered the injector, we can now use it.

  1. @Route("/")
  2. export class MainRoute {
  3. public async get(foo: Foo) {
  4. return foo.helloWorld() // will render { hello : "world" }
  5. }
  6. }

Here FuseBox magically resolved the first parameter, evaluated inject method.

inject method is resolved accordingly, respecting other injections recursively. So you can inject req res or next injections too.

Decorators

Define your decorator by providing a class with init function to a wrapper MethodDecorator

  1. import { MethodDecorator } from "fuse-http";
  2. export const Permissions = MethodDecorator<string>(class {
  3. init(req) {
  4. if (!req.query.foobar){
  5. throw { message : "Foobar must be there" }
  6. }
  7. }
  8. })

init will be constucted with all your injections including req and res from express

Usage in route

  1. @Route("/")
  2. class TestRoute {
  3. @Permissions("sdf")
  4. public async get() {
  5. return {ok : true};
  6. }
  7. }

Error handlers

A handler must have test method, where we test if that particular exception should be processed. For example, here is BaseHandler

  1. @ErrorHandler()
  2. export class ErrorBaseHandler {
  3. public express : ExpressData;
  4. public test(e){
  5. const res = this.express.res;
  6. if ( e instanceof ErrorNotFound){
  7. res.status(404).send({code : 404, message : e.message})
  8. }
  9. }
  10. }