728x90
src/app.js
import Koa from 'koa';
const app = new Koa();
const port = parseInt(`${process.env.PORT || '3000'}`, 10);
app.use((ctx) => {
ctx.body = 'Hello world!';
});
app.listen((port, () => {
console.log(`PORT: ${port}`);
});
export default app;
Koa 어플리케이션은 미들웨어의 배열로 구성되어 있다.
6번째 라인은 하나의 미들웨어인 것이다. 미들웨어를 계속 실행시키기 위해서는 next()
를 호출해야한다.
async/await
Koa에선 별도의 작업없이 async/await
를 바로 사용할 수 있다.
import Koa from 'koa';
const app = new Koa();
const port = parseInt(`${process.env.PORT || '3000'}`, 10);
app.use(async (ctx) => {
await ctx.body = 'Hello world!';
});
app.listen((port, () => {
console.log(`PORT: ${port}`);
});
export default app;
728x90
'웹 프레임워크 > Javascript - Koa.js' 카테고리의 다른 글
[Javascript] Koa.js 프로젝트 생성 및 ESLint설정 (0) | 2021.06.29 |
---|