Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mvc-middleware

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mvc-middleware

Mvc middleware for express like .Net Mvc

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
increased by133.33%
Maintainers
1
Weekly downloads
 
Created
Source

mvc-middleware

Mvc middleware for express like .Net Mvc

How to use

import express, { Router } from 'express';
import { MvcMiddleware } from 'mvc-middleware';

const app = express();
MvcMiddleware.connect(app, Router);

app.listen(3000, 'localhost');

By default, place for controllers is my-project/controllers. You can specify your controller location:

import express, { Router } from 'express';
import { MvcMiddleware } from 'mvc-middleware';

const app = express();

const directoryPath = path.join(__dirname, 'src', 'some-folder', 'controllers');
new MvcMiddleware(app, Router)
    .registerControllers(directoryPath)
    .run();

app.listen(3000, 'localhost');

Controller sample: my-project/controllers/users-controller.js

import { UserService } from '../domain/users';
import { ControllerBase } from './base/controller-base';

export class UsersController extends MvcController {
    static area = '/users';
    static get = {
        '/api/users/list': 'list',
        '/api/users/:userId': 'getById',

        '/users': 'userListPage',
        ':userId': 'userPage',
    }
    static post = {
        '/api/users/add': 'add',
    }

    constructor(request, response) {
        super(request, response);

        this.users = [{
            id: 1,
            name: 'user 1',        
        }];
    }

    // GET: /users
    userListPage() {
        return this.view('list');
    }

    // GET: /users/:userId
    userPage(userId) {
        return this.view('user');
    }

    // GET: /api/users/list
    list() {
        return this.ok(this.users);
    }

    // GET: /api/users/:userId
    getById(userId) {
        const user = this.users.find(user => user.id === userId);
        return this.ok(user);
    }

    // POST: /api/users/add
    add({ name }) {
        this.users.push({
            id: this.users.length + 1,
            name,
        });
        return this.ok('user is added');
    }
}

export default UsersController;

Default paths for views is my-project/public/views/*.html and my-project/public/views/<controller area>/*.html. You change it by overriding getViewPath(viewName, area) method of MvcController.

Controller API

ok method of MvcController returns data with 200 status code. You can pass plain text or object that will be transformed to json.

Keywords

FAQs

Package last updated on 16 Sep 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc