Minimal Koa Typescript example using @koa/router and koa-body
This example showcases how to make a simple Hello World server using Koa in Typescript.
import Router from '@koa/router';
import koaBody from 'koa-body';
import Koa from 'koa';
const router = new Router();
const app = new Koa();
app.use(koaBody());
router.get('/', (ctx) => {
ctx.body = "Hello world";
});
app
.use(router.routes())
.use(router.allowedMethods());
if (!module.parent) app.listen(3000);
{
"compilerOptions": {
"strict": true,
"target": "es2020",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"sourceMap": true,
"resolveJsonModule": true,
"isolatedModules": true,
"skipLibCheck": true,
"outDir": "./build",
"rootDir": "./",
"lib": [
"esnext"
],
"forceConsistentCasingInFileNames": true
},
"include": [
"**/*.ts",
"types/*.ts"
],
"exclude": [
"node_modules"
]
}
{
"name": "KoaTypescriptTest",
"version": "1.0.0",
"description": "",
"scripts": {
"start": "./node_modules/.bin/tsnd --respawn server.ts",
"build": "./node_modules/.bin/tsc -b tsconfig.json"
},
"dependencies": {
"@koa/router": "^12.0.0",
"koa": "^2.14.1",
"koa-body": "^6.0.1"
},
"devDependencies": {
"@types/koa": "^2.13.5",
"@types/koa__router": "^12.0.0",
"@types/node": "^18.11.17",
"ts-node-dev": "^1.1.8",
"typescript": "^4.9.4"
}
}