# Koa

## HTTP server with Router

Create a project.

```
$ mkdir koa-demo
$ cd koa-demo
$ npm init
$ npm install koa
$ npm install koa-router
$ npm install koa-body
```

It should result in `package.json` like this. I have added `start` script to easier starting up the service.

```
{
  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "koa": "2.5.0",
    "koa-router": "7.4.0"
  }
}
```

Then we need to create `index.js` that will start up the server and add router URL path mapping.

```
const Koa = require('koa');
const koaBody = require('koa-body');
const Router = require('koa-router');

const app = new Koa();
app.use(koaBody());
const router = new Router();

router.get('/', (ctx, next) => {
  ctx.body = 'test response'
});

app
  .use(router.routes())
  .use(router.allowedMethods());

app.listen(4444);
```

Then we can run the server:

```
$ npm start
> koa-test@1.0.0 start /Users/ondrej/projects/koa-demo
> node index.js
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ondrej-kvasnovsky-2.gitbook.io/nodejs-handbook/koa.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
