使用overnightjs typescript 注解开发expressjs 应用
overnightjs 提供了基于注解的expressjs应用开发,包含了比较全的express 开发支持,使用简单,以下是一个简单的试用
项目准备
项目使用pkg 进行了打包处理
- 初始化
yarn init -y
- 添加依赖
yarn add @overnightjs/core @overnightjs/logger express http-status-codes
yarn add @types/express pkg typescript --dev
- pacakge.json 文件
添加了npm script 以及pkg 打包的配置
{
"name": "ts-express-decrator",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "node dist/index.js",
"live": "tsc -w",
"package": "pkg --options expose-gc -d ."
},
"bin": "dist/index.js",
"pkg": {
"scripts": "dist/**/*.js"
},
"dependencies": {
"@overnightjs/core": "^1.6.8",
"@overnightjs/logger": "^1.1.8",
"express": "^4.17.1",
"http-status-codes": "^1.3.2"
},
"devDependencies": {
"@types/express": "^4.17.1",
"pkg": "^4.4.0",
"typescript": "^3.6.3"
}
}
- typescript config
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"removeComments": true,
"strict": true,
"rootDirs": [],
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
代码编写
- 代码结构
src
├── SampleServer.ts
├── UserController.ts
└── index.ts
- 代码说明
index.ts 为应用的入口,SampleServer.ts 为 express server 的定义,UserController.ts 是一个rest 服务的定义
SampleServer.ts 代码:
import * as bodyParser from 'body-parser';
import { Server } from '@overnightjs/core';
import { Logger } from '@overnightjs/logger';
import { UserController } from './UserController';
export class SampleServer extends Server {
constructor() {
super(process.env.NODE_ENV === 'development'); // setting showLogs to true
this.app.use(bodyParser.json());
this.app.use(bodyParser.urlencoded({extended: true}));
this.setupControllers();
}
private setupControllers(): void {
const userController = new UserController();
// super.addControllers() must be called, and can be passed a single controller or an array of
// controllers. Optional router object can also be passed as second argument.
super.addControllers([userController]/*, optional router here*/);
}
public start(port: number): void {
this.app.listen(port, () => {
Logger.Imp('Server listening on port: ' + port);
})
}
}
UserController.ts:
import { OK } from 'http-status-codes';
import { Controller, Get, Post } from '@overnightjs/core';
import { Logger } from '@overnightjs/logger';
import { Request, Response } from 'express';
@Controller('user/say-hello')
export class UserController {
@Get()
private get(req: Request, res: Response): any {
Logger.Info('get called');
return res.status(OK).json({
message: 'get_called',
});
}
@Post()
private post(req: Request, res: Response): any {
Logger.Info('post called');
return res.status(OK).json({
message: 'post_called',
});
}
}
- index.ts
import { SampleServer } from "./SampleServer";
let myServer = new SampleServer()
myServer.start(3000)
构建&&启动
- 构建
yarn live
- 打包
yarn packagee
- 运行打包的应用
mac 系统,当然打包是跨平台的
./ts-express-decrator-macos
[2019-10-08T07:01:21.168Z]: Server listening on port: 3000
- 访问效果
curl -i http://localhost:3000/user/say-hello
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 24
ETag: W/"18-aJruZ7f86jURaNXHzg+3fDFbLjs"
Date: Tue, 08 Oct 2019 07:02:02 GMT
Connection: keep-alive
{"message":"get_called"}%
说明
使用overnightjs 开发express 应用还是很方便的,当然 routing-controllers 也是一个不错的选择(更新还很频繁),而且typestack 团队开发
的好多typescript 的框架也是很好用的。
参考资料
https://github.com/seanpmaxwell/overnight
https://github.com/typestack/routing-controllers
使用overnightjs typescript 注解开发expressjs 应用的更多相关文章
- SpringMVC注解开发初步
一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...
- SpringMVC的注解开发入门
1.Spring MVC框架简介 支持REST风格的URL 添加更多注解,可完全注解驱动 引入HTTP输入输出转换器(HttpMessageConverter) 和数据转换.格式化.验证框架无缝集成 ...
- Struts2框架之-注解开发
Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...
- Java自定义注解开发
一.背景 最近在自己搞一个项目时,遇到可需要开发自定义注解的需求,对于没有怎么关注这些java新特性的来说,比较尴尬,索性就拿出一些时间,来进行研究下自定义注解开发的步骤以及使用方式.今天在这里记下, ...
- 基于Nodejs生态圈的TypeScript+React开发入门教程
基于Nodejs生态圈的TypeScript+React开发入门教程 概述 本教程旨在为基于Nodejs npm生态圈的前端程序开发提供入门讲解. Nodejs是什么 Nodejs是一个高性能Ja ...
- Annotation(四)——Struts2注解开发
Hibernate和Spring框架的开发前边总结了,这次看一下流行的MVC流程框架Struts2的注解开发吧.Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action ...
- Annotation(三)——Spring注解开发
Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...
- Annotation(一)——注解开发介绍
<p>在编程中,一直强调的一点就是注释的编写,注释的规范等等.尤其是越是核心,程序越复杂,逻辑越多的清空下,注释的编写对我们以后的阅读代码,维护软件起着至关重要的作用.一款软件有着好的注释 ...
- spring注解开发中常用注解以及简单配置
一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...
随机推荐
- Java学习:数据库连接池技术
本节内容 数据库连接池 Spring JDBC : JDBC Template 数据库连接池 1.概念:其实就是一个容器(集合),存放数据库连接的容器 当系统初始化好后,容器中会申请一些连接对象,当用 ...
- 带着canvas去流浪系列之八 碰撞【华为云技术分享】
[摘要] canvas动画-碰撞仿真 示例代码托管在:http://www.github.com/dashnowords/blogs 经过前面章节相对枯燥的练习,相信你已经能够上手canvas的原生A ...
- SQL系列(十一)—— 函数(function)
SQL中的函数也非常多,而且不同的DBMS提供了相应的特殊函数.但是常用的共性函数大致可以分为以下几种: 函数类型 函数 数值函数 1.算术计算:+.-.*./ 2.数值处理:ABS()绝对值处理.P ...
- go get 命令
示例: go get github.com/jinzhu/gorm 下载并安装gorm包. 远程代码库有github,GitLlab,Gogs 命令介绍说明: -fix : 比如,我的代码是一年前1. ...
- -Git 使用技巧 总结 MD
目录 目录 Bash下的快捷操作 常用命令 常用操作 移动光标 删除输入内容 Tab键的作用 Git默认Vim编辑器基本使用 Git 使用场景 合并多个commit:rebase -i[s] 删除多个 ...
- .net平台下对C#代码的编译
最近赶项目忽然想到一个问题,那就是在 .Net平台下的C#代码是怎么从源代码到机器可以识别的电脑的(只怪自己上学不好好读书,现在又要重补一遍了!!!) 话不多说直接上调研结果: 预习知识: 1: IL ...
- 坑人的Mysql5.7 (默认不支持Group By语句)(转)
部署项目时,项目启动完毕.点击有group by 查询语句时出现错误,界面中没有该有的数据.查询log日志发现错误 Expression #1 of SELECT list is not in GRO ...
- vue动画理解,进入、离开、列表过度和路由切换。
vue的动画对于很多初学者,甚至对很多老鸟来说也是很费劲,不容易控制的. 这篇文章讲vue动画的理解.其实没那么难. 动画理解 一个元素从A状态变成B状态,如果这个过程通过某种方式反应在视图上了,那么 ...
- vm-install 模版创建虚拟机
主要用到的信息有:模版id和存储id 通过存储名字 # xe vm-install template=[template_uuid] new-name-label="name" s ...
- OCR2:tesseract字库训练
由于tesseract的中文语言包“chi_sim”对中文字体或者环境比较复杂的图片,识别正确率不高,因此需要针对特定情况用自己的样本进行训练,提高识别率,通过训练,也可以形成自己的语言库. 工具: ...