使用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 ...
随机推荐
- FusionInsight大数据开发---sorl应用开发
sorl应用开发 要求: 了解Solr应用开发适用场景 熟悉Solr应用开发流程 熟悉并使用Solr常用API 理解Collection设计基本原则 应用开发实践 Solr简介 Solr是一个高性能, ...
- Python 中拼音库 PyPinyin 的用法【华为云技术分享】
[摘要] 最近碰到了一个问题,项目中很多文件都是接手过来的中文命名的一些素材,结果在部署的时候文件名全都乱码了,导致项目无法正常运行. 后来请教了一位大佬怎么解决文件名乱码的问题,他说这个需要正面解决 ...
- PTA 甲级 1139
https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 其实这道题目不难,但是有很多坑点! 首先数据 ...
- Linux(二)各种实用命令
继续Linux命令学习,没有什么捷径,每个命令都去敲几遍就熟悉了,第二篇学习的是一些比较实用类的命令,主要是从开发的角度进行学习,并不深入,话不多说,开始! 一.系统管理类 1.1 stat --st ...
- unable to retrieve container logs for docker kubernetes
参考 https://github.com/knative/docs/issues/300 This is what happens when the build is successful and ...
- rest-spring-boot-starter
rest-spring-boot-starter 基于spring boot,统一业务异常处理,统一返回格式包装 依赖 <dependency> <groupId>tk.fis ...
- Java 数据类型 & 变量与常量 & 注释
一.数据类型 1.数据类型分类 Java 的数据类型分为两大类: 基本数据类型:整数.浮点数.字符型.布尔型 引用数据类型(对象类型):类.数组,字符串.接口等. 2.基本数据类型 四类八种基本数据类 ...
- IVS_原理
智能视频分析技术指计算机图像视觉分析技术,是人工智能研究的一个分支,它在图像及图像描述之间建立映射关系,从而使计算机能够通过数字图像处理和分析来理解视频画面中的内容.智能视频分析技术涉及到模式识别.机 ...
- golang reflect知识集锦
目录 反射之结构体tag Types vs Kinds reflect.Type vs reflect.Value 2019/4/20 补充 reflect.Value转原始类型 获取类型底层类型 遍 ...
- 树莓派开机发送IP地址到邮箱
树莓派使用的wifi联网,在宿舍使用的是公共网络,不能设置静态ip,每次树莓派上电开机后ip地址可能会改变,所以让树莓派开机联网后自动发送ip地址到QQ邮箱 一.安装mutt和msmtp mutt: ...