Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jesusvilla/natural

Fastest Framework for NodeJS. Written in pure ES6+
https://github.com/jesusvilla/natural

cloudflare cloudflare-workers fast framework http js node rest-api serviceworker webserver workers

Last synced: 3 months ago
JSON representation

Fastest Framework for NodeJS. Written in pure ES6+

Awesome Lists containing this project

README

        

# Natural
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/donate/?hosted_button_id=CW2Z4CLAX2GVC)

Fastest Framework for Node.js. Written in pure javascript (ES6+)

- Created with the least possible code.
- Pure native HTTP Server.
- Based on Polka, Restana, Fastify, Express (for the creation of the Router).
- Based on NestJS (framework concepts and abstraction).
- Support Serverless: Workers Cloudflare, AWS Lambda, Google Cloud Functions

## Requirements
Node.js v14+ (v16+ for Workers)

## Installation (/example)

```
$ npm i -S natural-framework
```

## Example
> View examples folder

### NaturalRouter (Local Server)
```js
import NaturalRouter from 'natural-framework/router'
import * as HttpServer from 'natural-framework/server/uws'
// import * as HttpServer from 'natural-framework/server/node' // for Node.js native application

function createRoutes (router) {
router
.get('/', (_, response) => {
response.end('')
})
.get('/user/:id', (request, response) => {
response.end(request.params.id)
})
.post('/user', (request, response) => {
response.end('')
})
.route({
url: '/test/simple/:id',
method: 'GET',
type: 'json',
handler: (request, response) => {
// request.params, request.query, request.body, request.files
response.send({ id: request.params.id }, 'json')
}
})

/* router.route({
url: '/meet/auth',
method: 'GET',
type: 'json',
handler: (request, response) => {
const params = Object.assign({}, request.params, request.query)
response.send(params)
}
}) */

// or
/*
router.on('GET', '/station/test/simple/:id', (request, response) => {
// request.params, request.query, request.body, request.files
response.send({ id: request.params.id }, 'json')
})
*/
}

async function bootstrap () {
const router = new NaturalRouter({
server: HttpServer
/* ssl: {
key: path.join(__dirname, './security/cert.key'),
cert: path.join(__dirname, './security/cert.pem')
} */
})
try {
createRoutes(router)
const port = await router.listen(3000)
console.log(`Listen http://localhost:${port}`)
} catch (error) {
console.log('Error:', error)
}
}

bootstrap()
```

### NaturalRouter (Serverless)
> Workers Cloudflare, AWS Lambda, Google Cloud Functions (Yeah, Same code for any serverless!!)
```js
import NaturalRouter from 'natural-framework/router'
import * as HttpServer from 'natural-framework/server/worker' // for Workers Cloudflare
// import * as HttpServer from 'natural-framework/server/lambda' // for AWS Lambda
// import * as HttpServer from 'natural-framework/server/function' // Google Cloud Functions

const router = new NaturalRouter({
server: HttpServer
}

router
.get('/', (_, response) => {
response.end('')
})
.get('/user/:id', (request, response) => {
response.end(request.params.id)
})
.post('/user', (request, response) => {
response.end('')
})
.route({
url: '/test/simple/:id',
method: 'GET',
type: 'json',
handler: (request, response) => {
// request.params, request.query, request.body
return { id: request.params.id }
// or: response.send({ id: request.params.id }, 'json')
}
})

/* router.route({
url: '/test/simple/:id',
method: 'POST',
type: 'json',
handler: (request, response) => {
const params = Object.assign({}, request.params, request.query, request.body)
response.send(params)
}
})

// or

router.on('POST', '/test/simple/:id', (request, response) => {
const params = Object.assign({}, request.params, request.query, request.body)
response.send(params)
})

*/

export default {
fetch: router.run()
}
```

### NaturalJS
```js
import NaturalJS from 'natural-framework'
import * as HttpServer from 'natural-framework/server/uws'
import station from './station'

async function bootstrap () {
const app = new NaturalJS({
server: HttpServer,
modules: {
station
}
})

try {
const port = await app.listen(3000)
console.log(`Listen http://localhost:${port}`)
} catch (error) {
console.log('Error:', error)
}
}

bootstrap()
```

## Definitions

### Controller

```js
import { Controller, Accepts, Get, TypeJson } from 'natural-framework/common'

// Registered route: /test
@Controller('test')
class Test {
// Without path
// Registered route: /test/
@Get()
main () {
// Return string, automatic detected by Natural
return 'Welcome'
}

// With path
// Registered route: /test/simple
@Get('simple')
getSimple () {
// Return string
return 'Simple Main'
}

// With path, with params
// Registered route: /test/simple/:id
@Get('simple/:id')
// With arguments: id
@Accepts('id')
// Return type: json (application/json)
@TypeJson()
getSimpleId (id) {
return { id, type: typeof id }
}

// With path, with params
@Get('validator/:id')
// With arguments with validator: id (only type number)
@Accepts({ name: 'id', type: 'number' })
// Return type: json (application/json)
@TypeJson()
getIdWithValidator (id) {
return { id, type: typeof id }
}
}

export default Test
```

## Use (Contribute)

Start NaturalRouter (/example)
```
$ npm run dev:router
```

Start NaturalJS (/example)
```
$ npm run dev
```

## Benchmarks
> Coming soon...

## ToDo

- Providers: Services, Models, ...

## Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/donate/?hosted_button_id=CW2Z4CLAX2GVC)

## Thanks

Made with love for all nodejs developers
Inspired in my family

## License

GPL-3.0 License