npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

neumatter

v1.1.6

Published

A light-weight and quick http server framework. ES6.

Downloads

21

Readme

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()