Socket
Socket
Sign inDemoInstall

express-control

Package Overview
Dependencies
66
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    express-control

Simplified Express api for Node apps


Version published
Maintainers
1
Created

Readme

Source

Express control

Simplified Express wrapper API for Node apps.

Typings included.

Basic app

This a basic example of a Node app with Express control:

// main.ts
import { createApp } from "express-control"

async function main() {
  const App = createApp()
  App.start()
}

main()

By default, your app will run on port 8000. You can pass an object to createApp specifying the port you want to use:

createApp({ port: 3000 })

Controllers

Controllers handle requests. Routes will be handlend according to how you define your controllers' methods:

// controllers/app.ts
import { RestController } from "express-control"

export default RestController({
  async "get /"(req, res){
    return "hello control"
  }
  async "get /hello"(req, res){
    return {
      message:"hello"
    }
  }
})

Using a controller

To use a controller, add it as a controller to your app:

// main.ts
import { createApp } from "express-control"
import AppController from "./controllers/app"

async function main() {
  const App = createApp()
  App.controller("/", AppController)
  App.start()
}

main()

Now routes for / will be handled by the AppController:

If a methd name is /hello in that AppController, it will handle the route /hello

Methods support all the Express features and they should start with the method name they support

async "post /info"(req,res) {
  status(500)
  return "Error saving info"
}

They also support params:

async "get /user/:id"(req,res) {
  return {
    id: req.params.id
  }
}

If you want to customise the response, you can return a function instead of an object:

async "get /info"(req, res) {
  return () => {
    res.status(401).json({
      error: "please login"
    })
  }
}

That's it!

Feel free to contribute :-)

I will include examples for integrations with different frameworks and libraries: Next.js, socket.io, etc.

Keywords

FAQs

Last updated on 05 Dec 2021

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