Code files
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
'use strict'
|
||||
|
||||
const {join} = require('path')
|
||||
const Koa = require('koa')
|
||||
const serve = require('koa-static')
|
||||
const router = require('koa-router')()
|
||||
const index = require('./routes/index')
|
||||
|
||||
const app = new Koa()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
if (dev) {
|
||||
app.use(serve(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
router.use('/', index.routes())
|
||||
|
||||
app.use(router.routes())
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"koa": "^2.2.0",
|
||||
"koa-router": "^7.1.1",
|
||||
"koa-static": "^3.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
'use strict'
|
||||
|
||||
const router = require('koa-router')()
|
||||
|
||||
router.get('/', async function (ctx, next) {
|
||||
await next()
|
||||
const { title } = ctx.state
|
||||
ctx.body = `
|
||||
<html>
|
||||
<head>
|
||||
<title> ${title} </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> ${title} </h1>
|
||||
<p> Welcome to ${title} </p>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
}, async (ctx) => ctx.state = {title: 'Koa'})
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,23 @@
|
||||
'use strict'
|
||||
|
||||
const {join} = require('path')
|
||||
const Koa = require('koa')
|
||||
const serve = require('koa-static')
|
||||
const router = require('koa-router')()
|
||||
const index = require('./routes/index')
|
||||
|
||||
const app = new Koa()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
if (dev) {
|
||||
app.use(serve(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
router.use('/', index.routes())
|
||||
|
||||
app.use(router.routes())
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"koa": "^2.2.0",
|
||||
"koa-router": "^7.1.1",
|
||||
"koa-static": "^3.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
'use strict'
|
||||
|
||||
const router = require('koa-router')()
|
||||
|
||||
router.get('/', async function (ctx, next) {
|
||||
const title = await pretendDbLookup('title')
|
||||
ctx.body = `
|
||||
<html>
|
||||
<head>
|
||||
<title> ${title} </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> ${title} </h1>
|
||||
<p> Welcome to ${title} </p>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
})
|
||||
|
||||
function pretendDbLookup () {
|
||||
return Promise.resolve('Koa')
|
||||
}
|
||||
|
||||
module.exports = router
|
||||
@@ -0,0 +1,27 @@
|
||||
'use strict'
|
||||
|
||||
const Koa = require('koa')
|
||||
const serve = require('koa-static')
|
||||
const router = require('koa-router')()
|
||||
const {join} = require('path')
|
||||
const index = require('./routes/index')
|
||||
const answer = require('./middleware/answer')
|
||||
|
||||
const app = new Koa()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.use(answer())
|
||||
|
||||
if (dev) {
|
||||
app.use(serve(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
router.use('/', index.routes(), index.allowedMethods())
|
||||
|
||||
app.use(router.routes())
|
||||
app.use(router.allowedMethods())
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
@@ -0,0 +1,10 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = answer
|
||||
|
||||
function answer () {
|
||||
return async (ctx, next) => {
|
||||
ctx.set('X-Answer', 42)
|
||||
await next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"koa": "^2.2.0",
|
||||
"koa-router": "^7.1.1",
|
||||
"koa-static": "^3.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
'use strict'
|
||||
|
||||
const router = require('koa-router')()
|
||||
|
||||
router.get('/', async function (ctx, next) {
|
||||
await next()
|
||||
const { title } = ctx.state
|
||||
ctx.body = `
|
||||
<html>
|
||||
<head>
|
||||
<title> ${title} </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> ${title} </h1>
|
||||
<p> Welcome to ${title} </p>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
}, async (ctx) => ctx.state = {title: 'Koa'})
|
||||
|
||||
module.exports = router
|
||||
Reference in New Issue
Block a user