Contract-Model-Model-View (CMMV)
A minimalistic framework for building scalable and modular applications using TypeScript contracts.
Documentation •
Report Issue
Description
@cmmv/server
is inspired by the popular Express.js framework but has been entirely rewritten in TypeScript with performance improvements in mind. The project integrates common plugins like body-parser
, compression
, cookie-parser
, cors
, etag
, helmet
and serve-static
out of the box. Additionally, it plans to support any Express.js-compatible plugin in the near future.
Installation
Install the @cmmv/server
package via npm:
$ npm install @cmmv/server @cmmv/server-static
Quick Start
Below is a simple example of how to create a new CMMV application:
import cmmv, { json, urlencoded, serverStatic } from '@cmmv/server';
import etag from '@cmmv/etag';
import cors from '@cmmv/cors';
import cookieParser from '@cmmv/cookie-parser';
import compression from '@cmmv/compression';
import helmet from '@cmmv/helmet';
const app = cmmv({
});
const host = '0.0.0.0';
const port = 3000;
app.use(serverStatic('public'));
app.use(cors());
app.use(etag({ algorithm: 'fnv1a' }));
app.use(cookieParser());
app.use(json({ limit: '50mb' }));
app.use(urlencoded({ limit: '50mb', extended: true }));
app.use(compression({ level: 6 }));
app.use(
helmet({
contentSecurityPolicy: {
useDefaults: false,
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", 'example.com'],
objectSrc: ["'none'"],
upgradeInsecureRequests: [],
},
},
}),
);
app.set('view engine', 'pug');
app.get('/view', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!' });
});
app.get('/', async (req, res) => {
res.send('Hello World');
});
app.get('/json', async (req, res) => {
res.json({ hello: 'world' });
});
app.get('/user/:id', async (req, res) => {
res.send('User ' + req.params.id);
});
app.get('/users', async (req, res) => {
res.json(req.query);
});
app.post('/test', async (req, res) => {
console.log(req.body);
res.send('ok');
});
app.listen({ host, port })
.then(server => {
console.log(
`Listen on http://${server.address().address}:${server.address().port}`,
);
})
.catch(err => {
throw Error(err.message);
});
Features
- Performance Optimized: Faster and more efficient with improvements over Express.js.
- Built-in Plugins: Includes commonly used middleware like
compression
, body-parser
, cookie-parser
, and more. - TypeScript First: Fully written in TypeScript for better type safety and developer experience.
- Express.js Compatibility: Plans to support Express.js-compatible plugins.
- HTTP/2 Support: Native support for HTTP/2, improving speed and connection performance.
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.