Skip to content

rmindo/vindo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vindo Framework

A simple, fast, scalable, file-based routing web framework for NodeJS.

Features

  • Middleware
  • Flexible file-based routing
  • Clean and easy access of utilities and context
  • Dependency injections

Installation

npm install @vindo/core

Or

yarn add @vindo/core

Configuration

Vindo had a few configurations that can be defined in a file vindo.json in the project directory.

{
  "port": "8080",
  // Arbitrary configuration
  "exert": {
    "passthrough": {}
  },
  // Environment variables
  "env": {
    // Using env file
    "ENV_PATH": {
      "dev": ".env/dev",
      "prod": ".env/prod"
    }
  },
  // Local libraries (lib directory) to include to the global context
  "include": {
    "auth": "lib/auth"
  },
  // Default directory of routes
  routesDirectory: 'http',
}

Documentation

Full documentation coming soon.

Example

More examples here

Get Started

Create a project directory with src folder in it and inside the src folder, create a file and name it server.js then follow the example below to run the server.

Create Server and Middleware

import {server} from '@vindo/core'

const app = server()

app.set((req, res, next, ctx) => {
  ...
  next()
})
app.run()

Run the server

$ node ./src/server.js

Inject dependency to the global context

Injected dependency will be accessible anywhere in the routes and in any library recently added in the context through configuration.

app.run(({env}) => {
  return {
    db: new Database(env.DATABASE_NAME),
    ...
  }
})

Routing

Vindo router had few options for defining routes inside a file. To make a route, create a folder inside the src folder and name it http (the main directory for all routes) then create an index file.

Serving route with specific method

Methods can be exported directly or define it inside a function.

// File: ./src/http/index.js
// Path: http://example.com/
export function GET(req, res, ctx) {
  res.html('<h1>Home</h1>')
}
export function POST(req, res, ctx) {
  res.json({greetings: 'Hello'})
}
// File: ./src/http/contact.js
// Path: http://example.com/contact
export function contact(req, res) {
  return {
    GET(ctx) {
      res.html('<h1>Contact</h1>')
    },
    POST(ctx) {
      res.html('<h1>Contact</h1>')
    }
  }
}

Serving route inside sub-directory

/**
 * File: ./src/http/about/index.js
 * Path: http://example.com/about
 */
export function about(req, res, ctx) {
  res.html('<h1>About</h1>')
}
/**
 * File: ./src/http/about/team.js
 * Path: http://example.com/about/team
 */
export function team(req, res, ctx) {
  res.html('<h1>Team</h1>')
}

Aggregated and mixed routes

// ./src/http/index.js
export default function(ctx) {
  return {
    // Path: http://example.com/about
    about(req, res) {
      res.html('<h1>About</h1>')
    },
    // Path: http://example.com/
    GET(req, res) {
      res.html('<h1>Hello</h1>')
    }
  }
}

Accessing global context

The context will be imported once called.

// Access from a single route

/**
 * File: ./src/http/subscribers.js
 * Path: http://example.com/subscribers
 */
export async function POST(req, res, ctx) {
  const db = ctx.db // Injected dependency
  const auth = ctx.auth // Local library included in the vindo.json configutation

  const id = auth.hash(auth.random())
  const args = {
    id,
    email: req.body.email
  }
  const result = await db.create('subscribers', args)
  if(result) {
    res.json({status: 'Success'}, 201)
  }
}
// Access from default export of aggregated routes
export default function(ctx) {
  const db = ctx.db // Injected dependency
  const auth = ctx.auth // Local library included in the vindo.json configutation

  return {
    async POST(req, res) {
      ...
    },
  }
}

Contributing

There's more needs to be enhanced in this project, especially the response. We will share our contribution guidelines soon.

Change Log

Semantic Versioning

License

MIT

About

A simple, fast, scalable, file-based routing web framework for NodeJS.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published