0.4.4 • Published 11 months ago

@cmmv/middleware v0.4.4

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

Description

The @cmmv/middleware module is designed to integrate Express middlewares into CMMV applications. It is based on the original implementation of Express and the features offered by the senchalabs/connect project. This module allows for flexible and modular addition of middlewares, providing a similar experience to Express. It supports a wide range of common middlewares, including authentication, session handling, request body parsing, and more, making it easy to create routes and handle HTTP requests efficiently and in an organized manner. Furthermore, the module maintains compatibility with custom middlewares, enabling developers to extend and adapt its functionality according to their application's specific needs.

Installation

Install the @cmmv/middleware package via npm:

$ pnpm add @cmmv/middleware

Quick Start

Below is a simple example of how to create a new CMMV application:

import cmmv from '@cmmv/server';
import * as cors from 'cors';
import * as compression from 'compression';
import helmet from 'helmet';
import _ from "./index";

const app = cmmv();

app.use(_(cors()));
app.use(_(helmet()));
app.use(_(compression({ level: 6, threshold: 0 })));

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

app.listen({ host: "127.0.0.1", port: 3000})
.then(server => {
    console.log(
        `Listen on http://${server.address().address}:${server.address().port}`,
    );
})
.catch(err => {
    throw Error(err.message);
});;

Middleware Compatibility Checklist

MiddlewareStatusNotes
cors✅ WorkingMiddleware tested and working as expected.
helmet✅ WorkingMiddleware tested and working as expected.
compression❌ Not WorkingMiddleware not working due to issues with triggering onHeaders event properly.
cookie-session❌ Not WorkingMiddleware not functioning correctly; issues with session management and Set-Cookie.
express-session❌ Not WorkingMiddleware incompatible; issues with session handling and storage.
serve-static⏳ Not TestedMiddleware for serving static files not yet tested.
body-parser✅ WorkingMiddleware tested and working as expected.
morgan✅ WorkingMiddleware tested and working, logs requests correctly.
serve-favicon⏳ Not TestedMiddleware for serving favicons not yet tested.
method-override⏳ Not TestedMiddleware for method override not yet tested.
csurf⚠️ DeprecatedMiddleware deprecated; not tested.
multer⏳ Not TestedMiddleware for file uploads not yet tested.

Native CMMV Middleware

CMMV provides native implementations for several common middlewares with enhanced performance optimizations. These native implementations are prefixed with @cmmv/ and include:

  • @cmmv/body-parser (NPM)
  • @cmmv/compression (NPM)
  • @cmmv/cookie-parser (NPM)
  • @cmmv/cors (NPM)
  • @cmmv/etag (NPM)
  • @cmmv/helmet (NPM)
  • @cmmv/server-static (NPM)

Summary

  • Working: cors, helmet, body-parser, and morgan have been tested and work as expected.
  • Not Working: compression, cookie-session, and express-session have issues related to header management, session handling, or storage that need to be addressed.
  • Not Tested: serve-static, serve-favicon, method-override, and multer have not been tested yet.
  • Deprecated: csurf is deprecated and will not be tested.

Summary

  • Working: cors, helmet, body-parser, morgan, serve-favicon, method-override, and multer have been tested and work as expected.
  • Not Working: compression, cookie-session, express-session, and serve-static have issues related to header management, session handling, or file serving that need to be addressed.

If you encounter issues or have suggestions on how to improve compatibility, feel free to contribute or report them on GitHub.

Documentation

The complete documentation is available here.

Support

CMMV is an open-source project, and we are always looking for contributors to help improve it. If you encounter a bug or have a feature request, please open an issue on GitHub.

Stay in Touch

License

CMMV is MIT licensed.

0.4.4

11 months ago

0.4.3

11 months ago

0.4.2

11 months ago