Socket
Socket
Sign inDemoInstall

midd

Package Overview
Dependencies
8
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    midd

A light-weight, promise-based, middleware-driven and express-compatible web framework


Version published
Weekly downloads
2
Maintainers
2
Install size
80.7 kB
Created
Weekly downloads
 

Readme

Source

A light-weight, promise-based, middleware-driven and express-compatible web framework

Features

  • Use promise-based middlewares(support async/await)
  • Better error handle(see Error Handle)
  • Easily use express middlewares(see Migrate From Express)
  • The core is light-weight, all functionalities are driven by midd-* middlewares, like midd-router, midd-static
  • Support request forward: req.forward('/new/url')

Example

const middServer = require('midd');
const app = middServer();

app.use(async (req, resp, next)=>{
  console.log('1');
  await next();
  console.log('3');
})
app.use(async (req, resp, next)=>{
  try{
    let user = await User.queryDb();
    resp.end(user.name);
  }catch(e){
    resp.statusCode = 500;
    resp.end(e.message)
  }
  
  console.log('2');
})
app.listen(8080)

API

middServer()

Create an app server.

app.use(...middlewares)

Attach middlewares. A middleware is a function like (req, resp, next)=>Promise.

app.listen()

Same as node server.listen() function.

app.on('error', cb:(err, resp)=>void)

Catch uncaught error in middlewares

req.forward(url)

Forward the request to another url.

app.listener()

Used like http.createServer(app.listerner())

Middlewares

Error Handle

You can use app.on('error', cb) to catch the uncaught error, or try/catch with async/await in middlewares

Migrate From Express

Currently only support express middleware operations, like express.Router and express.use|all|get|post|...()

app.express.get('/a', (req, resp, next)=>{
  console.log(req.url);
  next()//no return here
})
app.express.use(require('compression')())
app.express.use(require('body-parser').json())

Contributing

Checkout the CONTRIBUTING.md if you want to help

License

Licensed under MIT

Copyright (c) 2017 Tian Jian

Keywords

FAQs

Last updated on 22 Feb 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc