Skip to content

Commit

Permalink
feat: koa
Browse files Browse the repository at this point in the history
  • Loading branch information
benyueer committed Jun 14, 2023
1 parent 178253c commit 5154889
Show file tree
Hide file tree
Showing 7 changed files with 2,616 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.DS_Store
node_modules
/dist


# local env files
.env.local
.env.*.local

# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*

# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "koa-study",
"version": "0.1.0",
"private": true,

"dependencies": {
"debug": "^4.1.1",
"koa": "^2.7.0",
"koa-bodyparser": "^4.2.1",
"koa-convert": "^1.2.0",
"koa-json": "^2.0.2",
"koa-logger": "^3.2.0",
"koa-onerror": "^4.1.0",
"koa-router": "^7.4.0",
"koa-static": "^5.0.0",
"koa-views": "^6.2.0",
"pug": "^2.0.3"
},
"devDependencies": {
"nodemon": "^1.19.1"
}
}
8 changes: 8 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
color: #00B7FF;
}
44 changes: 44 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const Koa = require('koa')
const app = new Koa()
const views = require('koa-views')
const json = require('koa-json')
const onerror = require('koa-onerror')
const bodyparser = require('koa-bodyparser')
const logger = require('koa-logger')

const index = require('./routes/index')
const users = require('./routes/users')

// error handler
onerror(app)

// middlewares
app.use(bodyparser({
enableTypes:['json', 'form', 'text']
}))
app.use(json())
app.use(logger())
app.use(require('koa-static')(__dirname + '/public'))

app.use(views(__dirname + '/views', {
extension: 'pug'
}))

// logger
app.use(async (ctx, next) => {
const start = new Date()
await next()
const ms = new Date() - start
console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
})

// routes
app.use(index.routes(), index.allowedMethods())
app.use(users.routes(), users.allowedMethods())

// error-handling
app.on('error', (err, ctx) => {
console.error('server error', err, ctx)
});

module.exports = app
23 changes: 23 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const router = require('koa-router')()

router.get('/', async (ctx, next) => {
await ctx.render('index', {
title: 'Hello Koa 2!'
})
})

router.get('/string', async (ctx, next) => {
ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
ctx.body = {
title: 'koa2 json'
}
})

router.post('/test', async (ctx, next) => {
ctx.body = {data : {msg: 'hello world'}}
})

module.exports = router
13 changes: 13 additions & 0 deletions src/routes/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const router = require('koa-router')()

router.prefix('/users')

router.get('/', function (ctx, next) {
ctx.body = 'this is a users response!'
})

router.get('/bar', function (ctx, next) {
ctx.body = 'this is a users/bar response'
})

module.exports = router
Loading

0 comments on commit 5154889

Please sign in to comment.