create-router

1.1.0 • Public • Published

Create Router

🎉 Lightweight, easy to use alternative to express

Usage

$ npx create-router [port]

Run the project any time from the index.js file. This file will run any files in your routes/ directory.

create a route by creating a file in your routes directory like this

// ping.js
const router = require("@router");

router.get("/ping", ({ res }) => {
  res.end("pong");
});

Additionally, you can add arguments and allow the requested path to extend infinitly far like this

// /ping/<args[0]>
router.get("/ping/$", ({ args, res }) => {
  res.end(args[0]);
});

// /ping/...
router.get("/ping/*", ({ res }) => {
  res.write("pong");
});

Additionally, you can use a very bare-bones WebSocket route and middleware

router.ws("/notify", (socket) => {
  socket.on("data", data => {
    const milliseconds = Number(data) * 1000;
    setInterval(() => {
      socket.write("ping!");
    }, milliseconds);
  });
});

router.use((req, res) => {
  res.setHeader("Content-Type", "text/plain");
});

There are also shorthand statements included for json reading and writing

const messages = [];

router.post("/message", ({ req, res }) => {
  req.json().then(message => {
    messages.push(message);
    res.json({
      messageData: message,
      message: "Your message was successfully posted"
    });
  });
});

Any unused routes will be automatically directed to the views/ folder.

Package Sidebar

Install

npm i create-router

Weekly Downloads

6

Version

1.1.0

License

ISC

Unpacked Size

7.19 kB

Total Files

5

Last publish

Collaborators

  • ephf