spress
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

Worst Idea I ever had

To recreate express library just for fun

Usage

Make a simple server

const app = new Server();

app.get("/", (req, res) => {
  res.json({ foo: "bar" });
});

app.listen(8080, () => console.log("Server Started..."));

Supports all requests types

app.get("/", (req, res) => {
  res.json({ foo: "bar" });
});

app.post("/", (req, res) => {
  res.json({ method: "POSR" });
});

app.delete("/", (req, res) => {
  res.json({ method: "delete" });
});

Param based routes

app.get("/:id", (req, res) => {
  const id = req.params.id
  res.json({ foo: "bar" , id:  id });
});

Support for query params

app.get("/:id", (req, res) => {
  const searchQuery = req.query.search
  res.json({ foo: "bar" , search:  searchQuery });
});

middleware support

// Simple logger middleware
export const logger = (req, res, next) => {
  console.log(req.method, req.url);
  next();
};

app.use(logger)

Supports Routers

const myRouter = new Router();

myRouter.all("/", (req, res) => {
  res.json({ hey: "default" });
});

app.addRouter("/router", myRouter);

Typescript support

app.get(
  "/generic",
  (req: Request<{ hey: string }, {}, { search: string }>, res) => {
    const hey = req.body.hey;           // Vaild Typescript
    const yeh = req.body.yeh;           // InVaild Typescript
    const search = req.query.search;    // Valid Typescript
    const hcraes = req.query.hcraes     // InValid Typescript
    return res.json({ hey, search });
  }
);

Body Parser

app.get("/", (req, res) => {
  const hey = req.body.hey
  res.json({ foo: "bar" , hey:  hey });
});

Package Sidebar

Install

npm i spress

Weekly Downloads

3

Version

0.1.2

License

MIT

Unpacked Size

62.8 kB

Total Files

47

Last publish

Collaborators

  • arun_joseph