-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
53 lines (42 loc) · 1.1 KB
/
app.js
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
50
51
52
53
const Koa = require('koa');
const json = require('koa-json');
const path = require('path');
const Router = require('koa-router');
const render = require('koa-ejs');
const app = new Koa();
const router = new Router();
const bodyParser = require('koa-bodyparser');
// Json prettier middleware
app.use(json());
// Bodyparser
app.use(bodyParser());
const names = ['Vito', 'Joe', 'Henry', 'Eddie', 'Leo'];
render(app, {
root: path.join(__dirname, 'views'),
layout: 'template',
viewExt: 'html',
cache: false,
debug: false
});
router.get('/', index);
router.get('/contact', showContact);
router.post('/addContact', addContact);
router.get('/koa', showKoa);
async function index(ctx){
await ctx.render('index', { title: 'My index page ;)', names });
}
async function showContact(ctx){
await ctx.render('contact');
}
async function addContact(ctx){
const { name } = ctx.request.body;
names.push(name);
ctx.redirect('/');
}
async function showKoa(ctx){
ctx.body = { msg: 'Hello from Koa!' }
}
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000, () => console.log('Server Started!'))