Skip to content

Commit bbb37d3

Browse files
authored
Merge pull request #36 from Swechhya/example-koa-project
Add example project for koa
2 parents 032cb25 + d9e62a2 commit bbb37d3

File tree

13 files changed

+5980
-0
lines changed

13 files changed

+5980
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ app.listen(port, () => console.log(`Example app listening on port ${port}!`));
109109

110110
1. [Basic Node Web Server (TypeScript)](examples/node-http-server-ts)
111111
2. [Simple Express Web Server (TypeScript)](examples/express-http-server-ts)
112+
3. [Simple Koa Web Server (JavaScript)](examples/koa-http-server-js)
112113

113114
## API Docs
114115

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"targets": {
7+
"node": true
8+
}
9+
}
10+
]
11+
]
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": ["eslint-config-leapfrog"],
3+
"parserOptions": {
4+
"sourceType": "module"
5+
},
6+
"env": {
7+
"es6": true,
8+
"browser": true,
9+
"node": true
10+
}
11+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist/
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Koa Example (JavaScript)
2+
3+
Sample project for with [koa](https://koajs.com//).
4+
5+
## Running
6+
7+
Install dependencies.
8+
9+
```bash
10+
$ yarn
11+
```
12+
13+
Run the koa app server.
14+
15+
```
16+
$ yarn start
17+
```
18+
19+
Now you can test it through `curl`.
20+
21+
```
22+
$ curl 'http://localhost:3000?a=20&b=30' && curl 'http://localhost:3000?a=10&b=50' && curl 'http://localhost:3000?a=50&b=100'
23+
```
24+
25+
**Output**
26+
27+
```
28+
2019-10-26T08:42:23.335Z [ INFO ] HTTP server listening on port 3000!
29+
30+
2019-10-26T08:42:31.037Z [ DEBUG ] f45b64f0 - Persisted a: 20
31+
2019-10-26T08:42:31.037Z [ DEBUG ] f45b64f0 - Persisted b: 30
32+
2019-10-26T08:42:31.038Z [ DEBUG ] f45b64f0 - Simulating delayed access
33+
2019-10-26T08:42:31.039Z [ DEBUG ] f45b64f0 - Calculated sum: 50
34+
2019-10-26T08:42:31.039Z [ DEBUG ] f45b64f0 - Persisted sum: 50
35+
2019-10-26T08:42:31.042Z [ INFO ] f45b64f0 - Response sent
36+
2019-10-26T08:42:31.078Z [ DEBUG ] f5d8bbdb - Persisted a: 10
37+
2019-10-26T08:42:31.079Z [ DEBUG ] f5d8bbdb - Persisted b: 50
38+
2019-10-26T08:42:31.079Z [ DEBUG ] f5d8bbdb - Simulating delayed access
39+
2019-10-26T08:42:31.079Z [ DEBUG ] f5d8bbdb - Calculated sum: 60
40+
2019-10-26T08:42:31.079Z [ DEBUG ] f5d8bbdb - Persisted sum: 60
41+
2019-10-26T08:42:31.079Z [ INFO ] f5d8bbdb - Response sent
42+
2019-10-26T08:42:31.100Z [ DEBUG ] 9f06d9ba - Persisted a: 50
43+
2019-10-26T08:42:31.100Z [ DEBUG ] 9f06d9ba - Persisted b: 100
44+
2019-10-26T08:42:31.100Z [ DEBUG ] 9f06d9ba - Simulating delayed access
45+
2019-10-26T08:42:31.100Z [ DEBUG ] 9f06d9ba - Calculated sum: 150
46+
2019-10-26T08:42:31.100Z [ DEBUG ] 9f06d9ba - Persisted sum: 150
47+
2019-10-26T08:42:31.101Z [ INFO ] 9f06d9ba - Response sent
48+
2019-10-26T08:42:33.039Z [ INFO ] f45b64f0 - Store contents: {"a":"20","b":"30","sum":50}
49+
2019-10-26T08:42:33.080Z [ INFO ] f5d8bbdb - Store contents: {"a":"10","b":"50","sum":60}
50+
2019-10-26T08:42:33.106Z [ INFO ] 9f06d9ba - Store contents: {"a":"50","b":"100","sum":150}
51+
52+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
webpack: config => {
5+
config.context = path.resolve(__dirname, 'src');
6+
config.entry = {
7+
main: './index.js'
8+
};
9+
config.output.path = path.resolve(__dirname, 'dist');
10+
config.resolve.extensions = ['.js', '.json'];
11+
12+
// When we build with webpack it messes up the global '__dirname'
13+
// given by node. To undo this we have to set this configuration
14+
// https://github.com/webpack/webpack/issues/1599
15+
config.node = {
16+
__dirname: false
17+
};
18+
19+
return config;
20+
}
21+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["eslint-config-leapfrog", "eslint-config-prettier"]
3+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "koa-http-server-js",
3+
"version": "1.0.0",
4+
"description": "A basic example for async-store and koa",
5+
"main": "index.js",
6+
"author": "Swechhya Bista",
7+
"license": "MIT",
8+
"scripts": {
9+
"lint": "eslint -c eslint.json 'src/*.js'",
10+
"lint:fix": "eslint --fix -c eslint.json 'src/*.js'",
11+
"start": "backpack",
12+
"build": "backpack build"
13+
},
14+
"dependencies": {
15+
"@leapfrogtechnology/async-store": "^1.0.2",
16+
"koa": "^2.10.0",
17+
"koa-request": "^1.0.0",
18+
"qs": "^6.9.0"
19+
},
20+
"devDependencies": {
21+
"babel-preset-es2015": "^6.24.1",
22+
"babel-preset-es2017": "^6.24.1",
23+
"backpack-core": "^0.8.4",
24+
"eslint": "^6.6.0",
25+
"eslint-config-leapfrog": "^2.0.1",
26+
"eslint-config-prettier": "^6.4.0"
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Koa from 'koa';
2+
3+
import * as store from '@leapfrogtechnology/async-store';
4+
5+
import * as logger from './logger';
6+
import { storeParams, calculateSum } from './middlewares';
7+
8+
const app = new Koa();
9+
10+
const port = process.env.PORT || 3000;
11+
12+
app.use(ctx => {
13+
store.initialize()(() => {
14+
storeParams(ctx);
15+
calculateSum();
16+
handleRequest(ctx);
17+
});
18+
});
19+
20+
app.listen(port, () => {
21+
logger.info(`HTTP server listening on port ${port}!\n`);
22+
});
23+
24+
/**
25+
* Handle incoming request.
26+
*
27+
* @param {Object} ctx
28+
*/
29+
function handleRequest(ctx) {
30+
const a = store.get('a');
31+
const b = store.get('b');
32+
const sum = store.get('sum');
33+
34+
ctx.body = `Sum of ${a}, ${b} = ${sum}\n`;
35+
logger.info('Response sent');
36+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as store from '@leapfrogtechnology/async-store';
2+
3+
/**
4+
* Get unique request id from store or return empty string.
5+
*
6+
* @returns {string}
7+
*/
8+
function getRequestId() {
9+
return (store.getId() || '').substring(0, 8);
10+
}
11+
12+
/**
13+
* Print log message to the stdout / stderr.
14+
*
15+
* @param {string} level
16+
* @param {string} message
17+
*/
18+
function log(level, message) {
19+
const timestamp = new Date().toISOString();
20+
const requestId = getRequestId();
21+
const line = `${timestamp} [ ${level} ] ${requestId ? `${requestId} - ` : ''}${message}\n`;
22+
23+
if (level === 'ERROR') {
24+
process.stderr.write(line);
25+
26+
return;
27+
}
28+
29+
process.stdout.write(line);
30+
}
31+
32+
/**
33+
* Write info logs and associated request id to stdout.
34+
*
35+
* @param {string} message
36+
*/
37+
export function info(message) {
38+
log('INFO', message);
39+
}
40+
41+
/**
42+
* Write debug logs and associated request id to stdout.
43+
*
44+
* @param {string} message
45+
*/
46+
export function debug(message) {
47+
log('DEBUG', message);
48+
}
49+
50+
/**
51+
* Write error logs and associated request id to stdout.
52+
*
53+
* @param {any} err
54+
*/
55+
export function error(err) {
56+
log('ERROR', err);
57+
}

0 commit comments

Comments
 (0)