@express.ts/stereotype
test package
Last updated 2 months ago by aboulmane .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @express.ts/stereotype 
SYNC missed versions from official npm registry.

@express.ts/stereotype

A simple framework for building efficient and scalable server-side applications, heavily inspired by Spring MVC.

app.module.ts

import {
  ComponentScan,
  ExpressBootApplication,
  IExpressApplication,
  Middleware
} from "@express/stereotype";
const config = require("../config/appConfig.json");
// Create Express server
@ComponentScan([
  path.join(__dirname, "./services/impl"),
  path.join(__dirname, "./controllers"),
])
@ExpressBootApplication({
  settings: [{
    key: "async", value: false }, {
    key: "port", value: process.env.PORT || 3000 }, {
    key: "httpPort", value: process.env.PORT || 3000 }, {
    key: "createForkWorkers", value: true }, {
    key: "views", value: path.join(__dirname, "../views") }, {
    key: "view engine", value: "pug"
  }],
  properties: [
    config
  ],
  middlewares: [
    compression(),
    lusca.xssProtection(true),
    express.static(path.join(__dirname, "public"), { maxAge: 31557600000 })
  ]
})
export class AppModule implements IExpressApplication {
  public expressApp: core.Express;
  public startServers: () => Promise<any>;
  public bootstrap: () => Promise<any>;
  createConnection () {
    // Connect to MongoDB
    // createConnection()
    //   .then(connection => console.log("Connect to MongoDB: ", connection.isConnected))
    //   .catch(errors => console.log(errors));
  }
  public $OnInit () {
    console.log(`$OnInit worker ${process.pid}`);
  }
  public $OnReady () {
    console.log(`$OnReady worker ${process.pid}`);
  }
  @Middleware
  setLocalsUser (req: any, res: any, next: any) {
    res.locals.user = req.user;
    next();
  }
  @Middleware
  goToDistanition (req: any, res: any, next: any) {
    // After successful login, redirect back to the intended page
    if (!req.user &&
      req.path !== "/login" &&
      req.path !== "/signup" &&
      !req.path.match(/^\/auth/) &&
      !req.path.match(/\./)) {
      req.session.returnTo = req.path;
    } else if (req.user &&
      req.path == "/account") {
      req.session.returnTo = req.path;
    }
    next();
  }
}
export const appModule = new AppModule();
export default appModule;

server.ts

import errorHandler from "errorhandler";
import appModule from "./app.module";
const { expressApp } = appModule;
appModule
  .bootstrap()
  .then((msg: any) => {
    console.info(msg);
    /**
     * Error Handler. Provides full stack - remove for production
     */
    expressApp.use(errorHandler());
    /**
     * Start Express server.
     */
    appModule.startServers()
      .then(msg => console.log(msg))
      .catch(err => console.error(err));
  });
export default appModule;

./controllers/UserController.ts

import { GetMapping, PostMapping, RequestMapping, RequestMethod } from "@express/router";
import { Autowired, Controller, PathVariable, Request, Response } from "@express/stereotype";
import IMailService from "../services/IMailService";
import IMetierService from "../services/IMetierService";
@Controller()
export default class UserController {
  @Autowired()
  protected mailService: IMailService;
  @Autowired()
  protected metierService: IMetierService;
  /**
   * GET /login
   * Login page.
   */
  @GetMapping("/login")
  getLogin (@Request() req: any,
            @Response() res: any) {
    console.log("getLogin(): ========>", this.metierService.getTemperature());
    if (req.user) {
      return res.redirect("/");
    }
    res.render("account/login", {
      title: "Login"
    });
  }
}

./services/IMetierService.ts

export default interface IMetierService {
  getTemperature (): number;
}

./services/impl/MetierService.ts

import { Service } from "@express/stereotype";
import IMetierService from "../IMetierService";
@Service()
export default class MetierService implements IMetierService {
  getTemperature(): number {
    return Date.now();
  }
}

Current Tags

  • 1.1.3                                ...           latest (2 months ago)

5 Versions

  • 1.1.3                                ...           2 months ago
  • 1.1.2                                ...           10 months ago
  • 1.1.0                                ...           a year ago
  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 6
Dependencies (5)
Dev Dependencies (4)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |