forked from rstropek/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
49 lines (43 loc) · 1.66 KB
/
Copy pathserver.ts
File metadata and controls
49 lines (43 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import * as cors from '@koa/cors';
import * as Koa from 'koa';
import * as bodyParser from 'koa-bodyparser';
import * as logger from 'koa-logger';
import * as Router from 'koa-router';
import * as views from 'koa-views';
import { FileSystemLoader } from 'nunjucks';
import * as path from 'path';
import { getAllPeople } from './people';
import { addTodo, deleteTodo, getAllTodo, getTodo, patchTodo } from './todo';
// Tip:
// For more complex applications, consider using a dependency injection
// framework like InversifyJS. There are plugins for express and koa (see also
// e.g. https://www.npmjs.com/package/inversify-koa-utils)
// Read more about routing at https://github.com/alexmingoia/koa-router
const router = new Router({prefix: '/api'});
router.get('/people', getAllPeople);
router.get('/todos', getAllTodo);
router.get('/todos/:id', getTodo);
router.post('/todos', addTodo);
router.patch('/todos/:id', patchTodo);
router.delete('/todos/:id', deleteTodo);
// Read more about koa at http://koajs.com/
const app = new Koa();
app.use(cors());
app.use(logger());
app.use(bodyParser());
app.use(router.routes());
// Read more about koa views at https://github.com/queckezz/koa-views
// Read more about Nunjucks at https://mozilla.github.io/nunjucks/
const viewPath = path.join(__dirname, 'views');
app.use(views(viewPath, {
map: {html: 'nunjucks'},
options: {loader: new FileSystemLoader(viewPath)}
}));
app.use(async (ctx, next) => {
// If nothing else was found, render index (assumption: single-page app)
await ctx.render('index');
});
const port: (number|string) = process.env.PORT || 8080;
app.listen(port, () => {
console.log(`Server is listening on port ${port}...`);
});