Git Product home page Git Product logo

eskey-srv's Introduction

eskey-srv

Nodejs micro http server with object notation routing. No extra dependencies are required.

Hello world example:

const EskeyServer = require('./lib/server');
const port = 8080
const app = new EskeyServer();

app.applyRoutes([{ method: 'GET', use: (req, res) => res.end("Hello world") }])

app.listen(port, () => process.stdout.write('Server is listening'));

Routing is based on provided array of routes. Interface of single route presented below:

interface Route {
  children?: Route[] 
  use?: Handler | Handler[],
  method?: string,
  path?: string,
  pathMatchFull?: boolean,
  redirectTo?: string,
}

All properties are optional: more properties filled -> the match will be more precise.

The server extends ClientRequest with params and queryParams. All params should be preceded by colon.

interface ServerResponse extends http.ServerResponse {}

interface ClientRequest extends http.IncomingMessage {
  queryParams: URLSearchParams,
  params: {
    [key: string]: string
  },
}

type Handler = (req: ClientRequest, res: ServerResponse) => any;

With that being said below is more advanced example:

const appRoutes: Route[] = [
  { 
    path: 'api',  use: someMiddleware, children: [
      { 
        path: 'users', children: [
          { 
            path: ':id', children: [
              { method: 'GET', use: usersController.getUser },
              { method: 'DELETE', use: [ usersController.auth, usersController.delete ] }
            ]
          },
          { method: 'GET', use: usersController.list }
        ]
      }
    ]
  },
  { path: '', pathMatchFull: true, use: (req, res) => res.end('Hello world') }
  { use: (_, res) => (res.statusCode=404, res.end()) }
]

const httpServer = http.createServer();
const app = new EskeyServer(appRoutes, httpServer)
  .listen(8080, () => process.stdout.write('Server is listening'))

Nesting paths needs to be accomplished by nesting Route objects with children property. Route.path can't have slash separator. Route handlers are executed sequentially until ServerResponse is finish.

eskey-srv's People

Contributors

kolodziejczak-sz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.