当前位置:网站首页>TypeScript-在koa中配置TS和使用koa-router
TypeScript-在koa中配置TS和使用koa-router
2022-06-11 07:54:00 【YY小怪兽】
详情可见
1.创建Node项目
npm init --y
2.生成配置tsconfig.json
npm install typescript -g
tsc --init
3.安装相关依赖
3-1.安装koa
npm install koa --save
3-2.安装node和koa的相关的头文件
npm install @types/node @types/koa --save-d
3-3.方便设置当前的开发状态
npm install cross-env --save
3-4.方便实时更新编译后的代码
npm install nodemon --save-d
4.配置package.json
"scripts"下添加如下代码
"dev": "cross-env NODE_ENV=dev nodemon -e ts --exec ts-node app.ts"
5.编写koa代码
// const Koa = require('koa');//通过Node Module导入
// import Koa from 'koa'//通过ES Module导入
import Koa = require('koa')//通过TS Module导入
const app = new Koa();
// response
app.use((ctx:any) => {
ctx.body = 'Hello Koa';
});
app.listen(3000, ()=>{
console.log('listen 3000 OK');
});
6.使用koa-router
6-1.安装相关依赖
安装koa-router
npm install koa-router --save
安装koa-router的头文件
npm install @types/koa-router --save-d
6-2.编写相关代码
index.ts
import Router = require("koa-router");
const router:Router = new Router();
router.get('/', (ctx:any)=>{
ctx.body = 'router index';
});
router.get('/home', (ctx:any)=>{
ctx.body = 'router home';
});
export default router;
app.ts
import Koa = require('koa')//通过TS Module导入
import index from './routers/index';
const app = new Koa();
app.use(index.routes());
app.listen(3000, ()=>{
console.log('listen 3000 OK');
});
边栏推荐
- [atcoder2306] rearranging (topology)
- 二本畢業,銀行外包測試工作 4 個月有餘。聊聊一些真實感受 ...
- Euler's theorem and its extension (with proof)
- Switch statement
- [atcoder2000] leftmost ball (dp+ combination number)
- [cluster] haproxy load balancing
- Implementation of queue (C language)
- Bladed入门教程(视频)
- Scrape captures 51job Recruitment Information (static page)
- E regression test of this featured app
猜你喜欢

C language to achieve three piece chess (not artificial mental retardation ha ha ha)

Lesson 1 about Xiaobai's C language

Xshell7 and xftp7 to continue using this program, you must apply the latest updates or use a new version

【IoT】项目管理:如何打造更好的跨职能团队?

Import on CSDN MD file

Scrape captures 51job Recruitment Information (static page)

After 4 years of naked resignation from the test, the test post of 15K interview was rubbed on the ground, and the result made me collapse and cry

如何做好空状态设计?来看这份全面总结

Black Qunhui dsm7.0.1 physical machine installation tutorial

2022.6.6 特长生模拟
随机推荐
C- print 99 multiplication table
避免list的并发修改异常的几种方式
Bladed入門教程(視頻)
2. Graduated from this course, and the bank has outsourced testing work for more than 4 months. Talk about some real feelings
C wechat upload form data
mpi
Bidirectional linked list simple template (pointer version)
远程办公经验分享 | 社区征文
Implementation of queue (C language)
Long dialogue in June 2017
About static keyword
Rabin Miller prime test
After 4 years of naked resignation from the test, the test post of 15K interview was rubbed on the ground, and the result made me collapse and cry
Using Tkinter to realize guessing numbers game
排序——归并排序
How to prepare for the new PMP syllabus exam?
排序——交换排序
[codeforces1019e] raining season
Classes and objects (Part 2)
【集群】haproxy负载均衡