npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

summer-boot

v0.3.22

Published

mvc to node

Downloads

16

Readme

summer-boot

一款轻量级的基于express的、使用DI(依赖注入)和IOC(控制反转)的NodeJS框架,模块间高内聚低耦合

并集成了vue-ssr只需要少量的配置即可开启vue的ssr渲染,Demo可参考下面连接

vue-ssr-template

使用

git clone https://github.com/weidao123/summer-boot-example

说明

  • 开发环境默认会扫描*/app*目录下的所有组件
  • 如需要在应用启动前或启动后做一些初始化工作,可在 /app/application.ts 这个文件里面实现 StarterHandler 接口
  • 应用启动时默认会启动一个Master进程以及Agent进程和cpu核数的Worker进程(这个可在配置文件中覆盖)
  • 应用启动前会加载 app/config/config.default.ts 这个配置文件,可用来覆盖默认的一些配置(下面会有可配置项)

装饰器

  • [x] Service (装饰一个服务,可被Autowrite注入)
  • [x] ErrorHandler (全局异常处理器)
  • [x] Component (组件,可被Autowrite注入)
  • [x] Autowrite (注入组件)
  • [x] Controller (控制器路由映射)
  • [x] Interceptor (请求/响应拦截器)
  • [x] RequestMapping (映射方法路由)
  • [x] Req (注入Express 的 Request对象)
  • [x] Res (注入Express 的 Response 对象)
  • [x] Query (注入接收到的query参数)
  • [x] Body (注入接收到的body参数)
  • [x] PathVariable (注入url上的参数)
  • [x] Get、Post、Put、Delete、Patch
  • [x] @Schedule("30 * * * * *") (定时任务)

装饰器 Demo

  • /app/controller/user.ts
import {
    Autowrite, Body,
    Controller,
    Delete,
    Get,
    Patch,
    PathVariable,
    Post,
    Put, Query,
    Req,
    RequestMapping,
    RequestMethod
} from "summer-boot";
import BaseService from "../service/BaseService";
import {Request} from "express";

@Controller({path: "/user"})
export default class UserController {

    // 可以通过名称、或者类型注入
    @Autowrite()
    private baseService: BaseService;

    @Get("/test2/:id")
    public test2(@PathVariable("id") id: string) {
        return { id };
    }

    @Get("/test1")
    public test1(@Req req: Request) {
        return {
            data: this.baseService.getPath(),
            ip: req.ip,
        };
    }

    @RequestMapping({ path: "/postmapping", method: RequestMethod.POST })
    public postMapping(@Body body) {
        return {
            data: body
        };
    }

    @RequestMapping("/getmapping")
    public getMapping(@Query query) {
        return {
            data: query
        };
    }

    @Get("/get")
    public get() {
        return "get";
    }

    @Post("/post")
    public post() {
        return "post";
    }

    @Put("/put")
    public put() {
        return "put";
    }

    @Patch("/patch")
    public patch() {
        return "patch";
    }

    @Delete("/delete")
    public del() {
        return "delete";
    }
}
  • /app/interceptor/login-interceptor.ts
import {Autowrite, Interceptor, InterceptorHandler} from "summer-boot";
import {Request, Response} from "express";
import BaseService from "../service/BaseService";

// 拦截器: 拦截/front/* 的所有请求
// 不传参数代表拦截所有
@Interceptor('/front/(.*)')
export default class LoginInterceptor implements InterceptorHandler {

    @Autowrite()
    private baseService: BaseService;

    // 响应前处理
    after(req: Request, res: Response, data: any): Object {
        console.log(this.baseService.getPath());
        return {"state": 200, msg: "success", data: data};
    }

    // 前置拦截
    before(req: Request, res: Response): boolean {
        return true;
    }
}
  • /app/interceptor/global-exception.ts
import {ErrorHandler, ExceptionHandler} from "summer-boot";

// 全局异常处理
@ErrorHandler()
export default class GlobalErrorHandler implements ExceptionHandler {
    // 统一返回异常信息
    public exception(req, res, e) {
        res.send("global exception handler -->" + e);
    }
}

文件上传

import {Controller, Multipart, Post, Req} from "summer-boot";

@Controller({ path: "/upload" })
export default class UploadController {

    @Post("/file")
    public async file(@Req req) {
        const multipart = await Multipart.parse(req);
        if (multipart.hasFile("file")) {
            return {
                msg: "文件不存在"
            }
        }
        // 保存并重命名
        await multipart.save(f => `${Date.now()-${f.originName}`);
        return "success";
    }
}

定时任务

  • 底层基于 node-schedule 实现

  • 定时任务只会在agent进程执行

@Schedule("30 * * * * *")
export default class LoggerSchedule implements ScheduleHandler{

    public job: ScheduleJob;

    public run(date: Date): void {
        console.log("定时任务被执行--->pid=" + process.pid);
    }
}

全局配置

  • 配置文件位于 app/config/config.default.ts

    export default {
      "port": 8080, // 启动端口
      "worker": 2,  // 启动进程数量
      "log": {
        "dir": "logs",  // 日志文件数据目录
        "name": "summer-boot-web.log", // 日志文件名称
        "level": "INFO",  // 输出级别
        "size": "1kb"   // 单个文件最大大小
      },
      "ssr": {    // 服务端渲染的一些配置
        "output": "dist",
        "template": "index.html"
      },
      "staticDir": "public",  // 静态文件
    }