A simple, fast, scalable, file-based routing web framework for NodeJS.
- Middleware
- Flexible file-based routing
- Clean and easy access of utilities and context
- Dependency injections
npm install @vindo/core
Or
yarn add @vindo/core
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',
}
Full documentation coming soon.
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.
import {server} from '@vindo/core'
const app = server()
app.set((req, res, next, ctx) => {
...
next()
})
app.run()
Run the server
$ node ./src/server.js
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),
...
}
})
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.
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>')
}
}
}
/**
* 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>')
}
// ./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>')
}
}
}
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) {
...
},
}
}
There's more needs to be enhanced in this project, especially the response. We will share our contribution guidelines soon.
MIT