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

merced-express-oop

v1.0.2

Published

Applying OOP to Express

Downloads

1

Readme

merced-express-oop

A library that allows OOP patterns when working with express.

The way this works is that this library create a Server class with basic defaults to get an app running quick.

import Server from "merced-express-oop";
// create server
const server = new Server()

// create Router
const router = server.Router()

//Routes
router.get("/", (req, res) => res.send("Hello World"))

// turn on server with cors, basic logging and the router
server.cors().basiclogs().routers(["/", router]).listen()

Server Constructor

new Server({PORT: 5555})

The Server constructor takes one argument a config object that uses the following properties.

  • PORT: the port to listen on
  • onListen: callback from server.listen, runs after server starts listening for requests
  • host: defaults to localhost

Methods

These methods exist on an instance of the server class, you can always use inheritance to override any of them, ideally make sure any overridden versions still return this so they can be chained together.

server.basiclogs()

Adds some very basic logging middleware you can use if not planning to use morgan or other solution (which can be registered in the middleware method). Chainable

server.cors(urls)

Add cors headers, with no arguments all traffic is allowed, can be passed an array of strings representing urls you want to white list. For more control over cors settings use the cors library and register it using the middleware method. Chainable

server.bodyParsers()

Enables the express.json and express.urlencoded middleware. Chainable

server.Router()

Returns an express Router object. Not chainable.

server.middleware(middleware, ["/endpoint", middlware], ...)

Register global middleware, can be passed a function or as a 2 element array. All arguments will be registered as middleware. Chainable.

server.routers(router, ["/endpoint", router], ...)

Register routers with the app, works like the middleware function. Chainable

server.staticDirs("directory", ["/endpoint", "directory"], ...)

Sets up static directories which can be passed as a string representing the directory to be served or a two element array of the endpoint and directory string. Chainable

server.listen

Turns on the server will either use the PORT env variable or the PORT variable passed into the config object to the constructor. It will run the onListen function if one is defined after starting server. (refer to constructor)

Extending the Server Class

If you'd like to extend the Server class be aware the class has two properties outside of it's methods.

  • this.app: the express application object
  • this.configs: the object passed to the contructor

With these two properties you should be able to do pretty much anything without needing to override the constructor.

For example

import Server from "merced-express-oop"
import mongoose from "mongoose"

class MyServer extends Server {
    databaseConnection(){
        mongoose.connect(this.configs.uri)
        return this
    }
}

const server = new MyServer({
    uri: "mongodb://localhost:12017/mydatabase"
    })

router.get("/", (req, res) => res.send("Hello World"))
const router = server.Router()

server.server.cors().basiclogs().databaseConnection().routers(["/", router]).listen()