neumatter
v1.1.6
Published
A light-weight and quick http server framework. ES6.
Downloads
21
Maintainers
Readme
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
- Installation
- Getting Started
- Middleware
- Neumatter/App
- NeuRouter
- NeuRoute
- NeuRequest
- (method)
request.get
- (method)
request.emitErr
- (method)
request.isValidEmail
- (getter)
request.protocol
- (getter)
request.isSecure
- (getter)
request.host
- (getter)
request.hostname
- (getter)
request.port
- (getter)
request.path
- (getter)
request.query
- (getter)
request.href
- (getter)
request.ip
- (getter)
request.URL
- (getter)
request.searchParams
- (prop)
request.params
- (prop)
request.app
- (method)
- NeuResponse
- (method)
response.get
- (method)
response.set
- (method)
response.status
- (method)
response.redirect
- (method)
response.send
- (method)
response.json
- (method)
response.html
- (method)
response.download
- (method)
response.file
- (method)
response.render
- (getter)
response.sent
- (getter)
response.isHead
- (getter)
response.hasContentLength
- (prop)
response.hasType
- (prop)
response.locals
- (prop)
response.viewer
- (method)
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 parseNeuRequest.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()