npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

morest-express

v1.1.2

Published

Elegante, decorators, express framework

Downloads

9

Readme

morest

Morest is an abstraction of express to use it with decorators. It was created to have a better readability of code.

WARNING

This is a first version of implementation and all features to corresponding with express are not implemented

Features

  • Create class controller
  • Create all routes (get, put, post, patch, delete)
  • Possibility to add middleware
  • Support of ejs render

Installation

This is a Node.js module.

Before installing, download and install Node.js. Node.js 0.10 or higher is required.

$ npm install morest-express

or

$ yarn add morest-express

How to use

Hello World example

import Morest, {GetMapping, Controller, Request, Response} from 'morest-express'

@Controller('')
class IndexController {
​
    @GetMapping('/')
    index(req: Request, res: Response) {
        res.send("Hello World!")
    }
}
​
const morest = new Morest()
​
morest.run(3000, () => {
    console.log("Listening at 3000")
})

Add a middleware

Create a class middleware

import { MiddlewareModel, Request, Response, NextFunction } from 'morest-express';

class HelloMiddleware implements MiddlewareModel {

    run(req: Request, res: Response, next: NextFunction) {
        console.log("hello i'm a middleware")
        next()
    }

}

Add it on your controller

import Morest, {MiddlewareController, GetMapping, Controller, Request, Response} from 'morest-express'

@MiddlewareController(HelloMiddleware)
@Controller('/api')
class IndexController {
​
    @GetMapping('/')
    index(req: Request, res: Response) {
        res.send("Hello World!")
    }
}

or a specific route

import Morest, {MiddlewareRoute, GetMapping, Controller, Request, Response} from 'morest-express'

@Controller('/api')
class IndexController {
​
    @MiddlewareRoute(HelloMiddleware)
    @GetMapping('/')
    index(req: Request, res: Response) {
        res.send("Hello World!")
    }
}

or global middleware for all route

const morest = new Morest()

morest.useMiddleware(HelloMiddleware)

morest.run(3000, () => {
    console.log("Listening at 3000")
})

Possibility to add other middle like json-parser or cors :

const morest = new Morest()

morest.use(bodyParser.json())
morest.use(cors())

morest.run(3000, () => {
    console.log("Listening at 3000")
})

You can add params to pass it inside Middleware like this

Middleware :

import { MiddlewareModel, Request, Response, NextFunction } from 'morest-express';

class HelloMiddleware implements MiddlewareModel {

    run(req: Request, res: Response, next: NextFunction) {
        console.log(this.params) // {foo: bar}
        const params = this.params as {foo: string}
        console.log(params.foo) // "bar"
        next()
    }

}

Route :

import Morest, {MiddlewareRoute, GetMapping, Controller, Request, Response} from 'morest-express'

@Controller('/api')
class IndexController {
​
    @MiddlewareRoute(HelloMiddleware, {foo: "bar"})
    @GetMapping('/')
    index(req: Request, res: Response) {
        res.send("Hello World!")
    }
}

List of decorators

| Decorators | Usage | Description | | ------ | ----------- | ----------- | | Controller | @Controller(path) | Set a class as a controller. Use path to have a base path for each route inside a controller | RouteMapping | @RouteMapping(method, path) | Set a function as route. The first parameter is the method and the second, the path. The path is optionnal and default value is '/' | GetMapping | @GetMapping(path) | Set a function as route get. The path is optionnal and default value is '/' | PostMapping | @PostMapping(path) | Set a function as route post. The path is optionnal and default value is '/' | PutMapping | @PutMapping(path) | Set a function as route put. The path is optionnal and default value is '/' | PatchMapping | @PatchMapping(path) | Set a function as route patch. The path is optionnal and default value is '/' | DeleteMapping | @DeleteMapping(path) | Set a function as route delete. The path is optionnal and default value is '/' | MiddlewareController | @MiddlewareController(Middleware, params) | Set a middle on the controller, each route will have a middleware. The middleware must be a class inheritance MiddlewareModel. Params is list of params will be accessible by this.params from Middleware class | MiddlewareRoute | @MiddlewareRoute(Middleware, params) | Set a middle on the route. The middleware must be a class inheritance MiddlewareModel. Params is list of params will be accessible by this.params from Middleware class

Morest-cli

You can also use the cli to generate middleware and controller.

To generate a controller :

$ npx morest-express --controller <path> 

You can add a --resources flag to generate also all resources route

$ npx morest-express --controller <path> --resources

| Method | URI | Action | | ------ | ----------- | ----------- | | GET | / | index | | GET | /:id | show | | GET | /create | create | | POST | / | store | | GET | /:id/edit | edit | | PUT | /:id | update | | DELETE | /:id | delete |

To generate a middleware :

$ npx morest-express --middleware <path> 

Path

The path must be like path.of.the.file

Will be generate :

  • ./controllers/path/of/the/FileController.ts for controllers. The route will be http://localhost:3000/path/of/the/file
  • ./middleswares/path/of/the/FileMiddleware.ts for middlewares

You can use --src to change the source directory, by default it's ./src and with --folder too you can specify other folder

$ npx morest-express --middleware path.of.file --src

Will be generate at ./src/middleswares/path/of/the/FileMiddleware.ts

$ npx morest-express --middleware path.of.file --src --folder code

Will be generate at ./code/middleswares/path/of/the/FileMiddleware.ts

EJS

To use EJS you can add this on your app.ts

const morest = new Morest()

morest.set("view engine", "ejs");
morest.set("views", path.join(root.path, 'views'));

morest.run(3000, () => {
    console.log("Listening at 3000")
})

And use it like express.

License

MIT