{"size": 1305, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "Nasfame/koa-crud", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "index.js", "max_issues_repo_name": "Nasfame/koa-crud", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "index.js", "max_forks_repo_name": "Nasfame/koa-crud", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.0483870968, "max_line_length": 91, "alphanum_fraction": 0.646743295}
const koa = require('koa')
const json = require('koa-json')
const koaRouter = require('koa-router')
const render = require('koa-ejs')
const bodyparser = require('koa-bodyparser')
const path = require('path')

const app = new koa()
const router = new koaRouter()
const port = 9897
let countries = ['India', 'UAE', 'US']

//Setting the middleware

app.use(json())
app.use(bodyparser())
app.use(router.routes()).use(router.allowedMethods())
//app.use(bodyParser.urlencoded({ extended: true })) for forms in html 

render(app, {
    root: path.join(__dirname, 'views'),
    layout: 'layout',
    viewExt: 'html',
    cache: false,
    debug: false
})

//Routes

router.get('/test', async ctx => ctx.body = 'Router is Working')

router.get('/', async ctx => {
    await ctx.render('index', {
        title: 'Countries I love',
        countries: countries
    })
})

router.get('/add', async ctx => {
    await ctx.render('add')
})

router.post('/add', add)

//Controllers
async function add(ctx) {
    const body = ctx.request.body
    console.log(body)
    const country = body.country
    countries.push(country)
    ctx.redirect('/')
}

//App

app.use(async ctx => ctx.body = { msg: "This route is currently not developed" }) //Default

app.listen(port, () => console.log(`Server has started ${port}`))



