Skip to content

derozgurg/ExpressJS-Typescript-Projector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ExpressJS-Typescript-Project-Template

MVC Patterned Typescript - ExpressJS Starter Projector

Before Start

$ npm install

or

$ yarn 

Start

$ npm start

Build

$ npm run build

Example

Controller, Actions, Routing, Security Policy All in One You don't need to create routing or security policy one by one

import * as express from 'express';
import ApiControllerBase, { ApiController, HttpAction } from '../../core/controller/ApiControllerBase';
import authenticatedMiddleware from '../../policy/authenticatedMiddleware';
import authorizationMiddleware from '../../policy/authorizationMiddleware';

/**
 * If you don't specify any path for this controller, it uses the controller name automatically for path
 * for this controller the path will be '/user'
 */
@ApiController({
    authenticatedMiddleware,
    authorizationMiddleware
    //path:'/user'
})
export default class UserController extends ApiControllerBase {

    //http://localhost:3001/user/userid
    @HttpAction({
        method: 'get',
        path: '/:id'
    })
    public get(request: express.Request, response: express.Response) {
        response.send('get user by id ');
    }


    //http://localhost:3001/user
    @HttpAction({
        method: 'get',
        path: '/',
        authenticated:true,
        roles:['super']
    })
    public list(request: express.Request, response: express.Response) {
        response.send('Users List');
    }


    //http://localhost:3001/user/userid/posts
    @HttpAction({
        method: 'get',
        path:'/:id/posts',
        authenticated:true,
        roles:['super','admin']
    })
    public userPosts(request: express.Request, response: express.Response) {

        response.send(`User posts for ${request.params.id}`);
    }

    /**
     * If you don't specify any path and action name is equals method, it automatically creates controller base path as a method
     * path will be '/user' for this action
     */
    @HttpAction({method: 'post'})
    public post(request: express.Request, response: express.Response) {
        console.log('Create user', this.path)
    }
}

About

Express.JS - Typescript starter projector (MVC patterned)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages