0.2.0 • Published 10 months ago

fressjs v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Express Route

Auto register handlers for Express, inspired by NextJS.

Supported:

  • Basic methods: GET, POST, PATH, OPTIONS, DELETE
  • Middleware using _middleware
  • Nested routes

Example folder structure

src
├── app
│   ├── server
│   │   ├── _middleware.ts
│   │   ├── index.ts // or get.ts
│   │   │── post.ts
│   │   │── users
│   │       ├── index.ts
│   ├── client
│   │   ├── _layout.ts
│   │   ├── index.ts
│   │   │── users
│   │       ├── index.ts
├── app.ts

Register middleware

// src/app/server/_middleware.ts
import { Request, Response, NextFunction } from "express";

export default function(req: Request, res: Response, next: NextFunction) {
  console.log("/admin/_middleware");
  next()
}

Register GET

// src/app/server/index.ts
import { Request, Response } from "express";

export default function(req: Request, res: Response) {

  res.json({
    message: "ok"
  })
}

Register nested route

// src/app/server/users/index.ts
import { Request, Response } from "express";

export default function(req: Request, res: Response) {

  res.json({
    message: "ok"
  })
}

Register routes

// app.ts
import path from "path"
import express from "express";
import register from "express-route-register";

const app = express();

register({
  app,
  // debug: true, // log registed handlers
  react: "/"
});


const host = process.env.HOST || "localhost";
const port = parseInt(process.env.PORT || "8080");

app.listen(port, host, () => {
  console.log(`Server started ${host}:${port}`);
})
0.2.0

10 months ago