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

express-fw

v0.1.10

Published

A simple wrapper for expressa simple wrapper for express

Downloads

9

Readme

express-fw

Usage

yarn add express-fw
import { createApp } from "express-fw"

export default createApp()

Boot

Usage express-fw.config.json

{
  "boot": ["my-boot"]
}

and create file boot in src/<name boot>

src/my-boot.ts:

import { boot } from "express-fw"

export default boot(() => {
  return (err, req, res, next) => {
    console.log("listened request on " + req.url)
    next()
  }
})

Router & Middleware

And now in the routes directory let's create your routes. express-import-routes will import all of them for you

project
└───routes
│   │   index.js
│   │
│   └───user
│       │   _id
│           └─── index.js
│  
└───app.js
└───package.json
└───yarn.lock

equivalent to

const express = require("express")
const app = express()

app.route("/", require("./routes/index.js"))
app.route("/user/:id", require("./routes/_id/index.,js"))

app.listen(8080, err => {
  if ( err ) {
    console.error(err)
  } else {
    console.log("App it runing on port 8080.")
  }
})

The file naming rules for configuring routers are the same as nuxtjs. Please refer here Nuxt router system

Route file writing rules

The route file in /routes requires you to export some function to render the route

index.js

exports.get = (req, res) => {
  req.end(`Hello!. This is a route /`)
}

You can exports. [get | post | put | delete | options] according to the method you want to listen to

The above example is equivalent to

const router = require("express").Router()

router.route("/").get((req, res) => {
  req.end(`Hello!. This is a route /`)
})

module.exports = router

** If you use an additional plugin eg multer you only need to exports an array **

const upload = multer({ dest: 'uploads/' })

exports.post = [upload.single('avatar'), function (req, res) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
}]

Middleware

Add stronger support with middleware.

You can now export the middleware to tell the plugin that you want it to apply the middleware to this route.

exports.middleware = ["auth"]

exports.get = (req, res) => {
  req.end(`Welcome back ${req.user.name}!`)
}

middleware/auth.js

module.exports = (req, res, next) => {
  try {
    if ( req.headers.authorization ) {
      req.user = jwt.verify(req.headers.authorization, SERKET_KEY)
      next()
    } else {
      throw new Error("NO_TOKEN")
    }
  } catch(err) {
    console.log( err )
    next("route")
  }
}

Specify local middleware

You can now specify each middleware for each router.

const upload = multer({ dest: 'uploads/' })

exports.post = [upload.single('avatar'), function (req, res) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
}]

or

exports.middleware = {
  post: upload.single('avatar'),
}

exports.post = function (req, res) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
}

Register

I added 2 methods for you to register the plugin to know this is a custom method. it can also combine with other modules like multer.

app.js

const express = require("express")
const multer = require("multer")
const importRoutes = require("express-import-routes")
const { registerMiddleware } = importRoutes

const app = express()

const upload = multer({ dest: "uploads/" })

registerMiddleware("file-avatar", upload.single("avatar"))

app.use(importRoutes())

app.listen(8080, err => {
  if ( err ) {
    console.error(err)
  } else {
    console.log("App it runing on port 8080.")
  }
})

Typescript

import { exposeRouter } from "express-import-routes"

export default exposeRouter({
  middleware: {
    post: upload.single('avatar'),
  },
  post (req, res) {
    // req.file is the `avatar` file
    // req.body will hold the text fields, if there were any
  }
})