1.1.6 • Published 8 months ago

neumatter v1.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

plot plot plot JavaScript Style Guide

A quick http server framework. ES6 async/await and ECMAScript modules.

Features:

  • ECMAScript modules.
  • Latest javascript features.
  • Support for global fetch.
  • Built in support for using dot env files.
  • Built in configurable headers.
  • Built in logger.
  • Support for file-based routing.
  • Can add replacer to file-based routing.
  • Async/Await.
  • Centralized error handling.

Table of Contents

Install

npm i neumatter --save

Getting Started

There are two options, using a configuration file or by using the constructor.

Configuration File: neumatter.config.json in root folder.

"env" is the options object, that will be passed to the constructor. "routes" required. path to routes/pages directory. "middleware" optional. path to middleware directory.

{
  "env": {
    "port": 8080,
    "static": "./public",
    "viewer": {
      "views": "./views",
      "blocks": "./views/blocks",
      "layouts": "./views/layouts"
    }
  },
  "middleware": "./server/middleware",
  "routes": "./server/routes"
}
import Neumatter from 'neumatter'

// loads the configuration and returns Promise<Neumatter>
const app = await Neumatter.load()

app.listen()

Constructor:

Options:

  • parseMethod: string Method that will be used to parse NeuRequest.body.
  • errorMiddleware: function Error Handler.
  • proxy: boolean Trust proxy.
  • env: string Often set as 'development' or 'production'.
  • port: number Port that the application will use.
  • host: string Hostname.
  • static: string Path to static folder that will be scoped to '/'.
  • context: object User defined object, that adds context to application.
  • configureHeaders: object
  • views: string Path to view folder.
  • viewer: object
  • viewer.views: string Path to views folder.
  • viewer.layouts: string Path to layouts folder.
  • viewer.defaultData: object User defined object, that adds data by default to a rebdered view.
  • logger: object
  • logger.name: string Name of the logger.
  • logger.dir: string Path to folder to hold logs.
  • logger.cacheSize: number Max size of logs to cache before writing to logs.
  • logger.virtual: boolean If logger is virtual only.
  • logger.json: boolean If logger should use json.
import Neumatter from 'neumatter'

const app = new Neumatter()

app.get('/', (req, res) => {
  res.send('Hello World')
})

app.listen()

Adding Routers & Middleware:

import Neumatter from 'neumatter'
import productRouter from './routes/products.js'
import productMiddlewareFn from './middleware.js'
import middlewareFn from './middleware.js'

const app = new Neumatter()

await app.use({
  middleware: [middlewareFn]
})

await app.use({
  path: '/products',
  middleware: [productMiddlewareFn],
  router: productRouter
})

app.listen()

Middleware

MiddlewareFn: (request, response, next)

  • request: NeuRequest
  • response: NeuResponse
  • next: NextFunction
const middlewareFn = (req, res, next) => {
  // do something
  next() // call NextFunction
}

ResponseFn: (request, response, next?)

  • request: NeuRequest
  • response: NeuResponse
  • next: NextFunction
const responseFn = (req, res) => {
  // do something
  res.json({ data: 'Hello World!' }) // send response
}

Neumatter/App

neumatter.use(data: { path?, middleware, router? })

  • data.path: string|null
  • data.middleware: Array<MiddlewareFn>
  • data.router: NeuRouter
import Neumatter from 'neumatter'
import productRouter from './routes/products.js'

const app = new Neumatter()

const middlewareFn = (req, res, next) => {
  // do something
  next() // call NextFunction
}

await app.use({
  middleware: [middlewareFn]
})

await app.use({
  path: '/products',
  middleware: [middlewareFn],
  router: productRouter
})

neumatter.useMany(prop: [data: { path?, middleware, router? }])

  • prop: Array<data>
  • data.path: string|null
  • data.middleware: Array<MiddlewareFn>
  • data.router: NeuRouter
import Neumatter from 'neumatter'
import productRouter from './routes/products.js'

const app = new Neumatter()

const middlewareFn = (req, res, next) => {
  // do something
  next() // call NextFunction
}

await app.useMany([
  {
    middleware: [middlewareFn]
  },
  {
    path: '/products',
    middleware: [middlewareFn],
    router: productRouter
  }
])

neumatter.listen(options?: { port?, host? })

  • options.port: number Port that the server will run on.
  • options.host: string Set the servers host. Defaults to localhost.

neumatter.listener()

The function that will be called on server requests. Creating a server and using the function manually will skip the neumatter.init function.

import Neumatter from 'neumatter'
import http from 'http'

const app = new Neumatter()

const server = http.createServer(Neumatter.serverOptions, app.listener())

neumatter.init(serverFn)

  • serverFn: typeof http.createServer
import Neumatter from 'neumatter'
import http from 'http'

const app = new Neumatter()

const server = app.init(http.createServer)

neumatter['METHOD'](path, middlewareFn|responseFn)

  • path: string Path for url lookup.
  • middlewareFn|responseFn: MiddlewareFn|Array<MiddlewareFn>|ResponseFn
  • METHODS:
    • get
    • post
    • put
    • patch
    • trace
    • options
    • connect
    • delete
import Neumatter from 'neumatter'
import http from 'http'

const app = new Neumatter()

app.get('/', (req, res) => {
  res.json({ data: 'Hello World!' })
})

app.post('/page',
  (req, res, next) => {
    if (!req.body.name) res.redirect('/')
    next()
  },
  (req, res) => {
    // do something
    res.send('success')
  }
)

Neumatter.load()

The function to load the configuration file and return the application.

import Neumatter from 'neumatter'

const app = await Neumatter.load()

app.listen()

Neumatter.Logger

The class that can be used to create a new Logger instance.

import Neumatter from 'neumatter'

const logger = new Neumatter.Logger({ virtual: true })

Neumatter.Router

The class that can be used to create a new Router instance.

import Neumatter from 'neumatter'

const router = new Neumatter.Router()
1.1.6

8 months ago

1.1.5

8 months ago

1.1.4

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.0-beta.7

2 years ago

1.0.0-beta.6

2 years ago

1.0.0-beta.5

2 years ago

1.0.0-beta.4

2 years ago

1.0.0-beta.3

2 years ago

1.0.0-beta.2

2 years ago

1.0.0-beta.1

2 years ago

1.0.0-beta.0

2 years ago