Chapter 7: initial samples
This commit is contained in:
parent
bcf782f7dc
commit
adb2130b38
@ -1,19 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const express = require('express')
|
||||
const {join} = require('path')
|
||||
const index = require('./routes/index')
|
||||
|
||||
const app = express()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
if (dev) {
|
||||
app.use(express.static(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
app.use('/', index)
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {Router} = require('express')
|
||||
const router = Router()
|
||||
const views = {
|
||||
index: require('../views/index')
|
||||
}
|
||||
|
||||
router.get('/', function(req, res, next()) {
|
||||
const title = 'Express'
|
||||
res.send(views.index({title}))
|
||||
next()
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@ -1,14 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = function indexView ({title}) {
|
||||
return `<html>
|
||||
<head>
|
||||
<title> ${title} </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> ${title} </h1>
|
||||
<p> Welcome to ${title} </p>
|
||||
</body>
|
||||
</html>`
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const express = require('express')
|
||||
const {join} = require('path')
|
||||
const index = require('./routes/index')
|
||||
|
||||
const app = express()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.set('views', join(__dirname, 'views'))
|
||||
app.set('view engine', 'ejs')
|
||||
|
||||
if (dev) {
|
||||
app.use(express.static(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
app.use('/', index)
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {Router} = require('express')
|
||||
const router = Router()
|
||||
|
||||
router.get('/', function(req, res) {
|
||||
const title = 'Express'
|
||||
res.render('index', {title: 'Express'})
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@ -1,10 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title> <%= title %> </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> <%= title %> </h1>
|
||||
<p> Welcome to <%= title %> </p>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,42 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const hapi = require('hapi')
|
||||
const inert = require('inert')
|
||||
const vision = require('vision')
|
||||
const ejs = require('ejs')
|
||||
const routes = {
|
||||
index: require('./routes/index'),
|
||||
devStatic: require('./routes/dev-static')
|
||||
}
|
||||
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
const server = new hapi.Server()
|
||||
|
||||
server.connection({
|
||||
host: 'localhost',
|
||||
port: port
|
||||
})
|
||||
|
||||
const plugins = dev ? [vision, inert] : [vision]
|
||||
server.register(plugins, start)
|
||||
|
||||
function start (err) {
|
||||
if (err) throw err
|
||||
|
||||
server.views({
|
||||
engines: { ejs },
|
||||
relativeTo: __dirname,
|
||||
path: 'views'
|
||||
})
|
||||
|
||||
routes.index(server)
|
||||
|
||||
if (dev) routes.devStatic(server)
|
||||
|
||||
server.start((err) => {
|
||||
if (err) throw err
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = devStatic
|
||||
|
||||
function devStatic (server) {
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: '/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: 'public'
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = index
|
||||
|
||||
function index (server) {
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: '/',
|
||||
handler: function (request, reply) {
|
||||
const title = 'Hapi'
|
||||
reply.view('index', {title})
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title> <%= title %> </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> <%= title %> </h1>
|
||||
<p> Welcome to <%= title %> </p>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,28 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {join} = require('path')
|
||||
const Koa = require('koa')
|
||||
const serve = require('koa-static')
|
||||
const views = require('koa-views')
|
||||
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
|
||||
|
||||
app.use(views(join(__dirname, 'views'), {
|
||||
extension: 'ejs'
|
||||
}))
|
||||
|
||||
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}`)
|
||||
})
|
||||
@ -1,19 +0,0 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ejs": "^2.5.6",
|
||||
"koa": "^2.2.0",
|
||||
"koa-router": "^7.1.1",
|
||||
"koa-static": "^3.0.0",
|
||||
"koa-views": "^6.0.1"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const router = require('koa-router')()
|
||||
|
||||
router.get('/', async function (ctx, next) {
|
||||
await next()
|
||||
await ctx.render('index')
|
||||
}, async (ctx) => ctx.state = {title: 'Koa'})
|
||||
|
||||
|
||||
// simplified:
|
||||
// router.get('/', async (ctx, next) => {
|
||||
// await ctx.render('index', {title: 'Koa'})
|
||||
// })
|
||||
|
||||
module.exports = router
|
||||
|
||||
@ -1,10 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title> <%= title %> </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> <%= title %> </h1>
|
||||
<p> Welcome to <%= title %> </p>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,28 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {join} = require('path')
|
||||
const express = require('express')
|
||||
const pino = require('pino')()
|
||||
const logger = require('express-pino-logger')({
|
||||
instance: pino
|
||||
})
|
||||
const index = require('./routes/index')
|
||||
|
||||
const app = express()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.set('views', join(__dirname, 'views'))
|
||||
app.set('view engine', 'ejs')
|
||||
|
||||
app.use(logger)
|
||||
|
||||
if (dev) {
|
||||
app.use(express.static(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
app.use('/', index)
|
||||
|
||||
app.listen(port, () => {
|
||||
pino.info(`Server listening on port ${port}`)
|
||||
})
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {Router} = require('express')
|
||||
const router = Router()
|
||||
|
||||
router.get('/', function (req, res) {
|
||||
const title = 'Express'
|
||||
req.log.info(`rendering index view with ${title}`)
|
||||
res.render('index', {title: 'Express'})
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@ -1,10 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title> <%= title %> </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> <%= title %> </h1>
|
||||
<p> Welcome to <%= title %> </p>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,25 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {join} = require('path')
|
||||
const express = require('express')
|
||||
const morgan = require('morgan')
|
||||
const index = require('./routes/index')
|
||||
|
||||
const app = express()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.set('views', join(__dirname, 'views'))
|
||||
app.set('view engine', 'ejs')
|
||||
|
||||
app.use(morgan('common'))
|
||||
|
||||
if (dev) {
|
||||
app.use(express.static(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
app.use('/', index)
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
@ -1,17 +0,0 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ejs": "^2.5.6",
|
||||
"express": "^4.15.2",
|
||||
"morgan": "^1.8.1"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {Router} = require('express')
|
||||
const router = Router()
|
||||
|
||||
router.get('/', function (req, res) {
|
||||
const title = 'Express'
|
||||
res.render('index', {title: 'Express'})
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@ -1,10 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title> <%= title %> </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> <%= title %> </h1>
|
||||
<p> Welcome to <%= title %> </p>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,28 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {join} = require('path')
|
||||
const express = require('express')
|
||||
const pino = require('pino')()
|
||||
const logger = require('express-pino-logger')({
|
||||
instance: pino
|
||||
})
|
||||
const index = require('./routes/index')
|
||||
|
||||
const app = express()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.set('views', join(__dirname, 'views'))
|
||||
app.set('view engine', 'ejs')
|
||||
|
||||
app.use(logger)
|
||||
|
||||
if (dev) {
|
||||
app.use(express.static(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
app.use('/', index)
|
||||
|
||||
app.listen(port, () => {
|
||||
pino.info(`Server listening on port ${port}`)
|
||||
})
|
||||
@ -1,19 +0,0 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ejs": "^2.5.6",
|
||||
"express": "^4.15.2",
|
||||
"express-pino-logger": "^2.0.0",
|
||||
"pino": "^4.2.4",
|
||||
"pino-debug": "^1.0.3"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {Router} = require('express')
|
||||
const router = Router()
|
||||
|
||||
router.get('/', function (req, res) {
|
||||
const title = 'Express'
|
||||
req.log.info(`rendering index view with ${title}`)
|
||||
res.render('index', {title: 'Express'})
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@ -1,10 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title> <%= title %> </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> <%= title %> </h1>
|
||||
<p> Welcome to <%= title %> </p>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,35 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const express = require('express')
|
||||
const {join} = require('path')
|
||||
const winston = require('winston')
|
||||
const expressWinston = require('express-winston')
|
||||
const index = require('./routes/index')
|
||||
const logger = new winston.Logger({
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
json: true
|
||||
})
|
||||
]
|
||||
})
|
||||
|
||||
const app = express()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.set('views', join(__dirname, 'views'))
|
||||
app.set('view engine', 'ejs')
|
||||
|
||||
app.use(expressWinston.logger({
|
||||
winstonInstance: logger
|
||||
}))
|
||||
|
||||
if (dev) {
|
||||
app.use(express.static(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
app.use('/', index)
|
||||
|
||||
app.listen(port, () => {
|
||||
logger.info(`Server listening on port ${port}`)
|
||||
})
|
||||
@ -1,18 +0,0 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ejs": "^2.5.6",
|
||||
"express": "^4.15.2",
|
||||
"express-winston": "^2.3.0",
|
||||
"winston": "^2.3.1"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {Router} = require('express')
|
||||
const router = Router()
|
||||
|
||||
router.get('/', function(req, res) {
|
||||
const title = 'Express'
|
||||
res.render('index', {title: 'Express'})
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@ -1,10 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title> <%= title %> </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> <%= title %> </h1>
|
||||
<p> Welcome to <%= title %> </p>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,50 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const hapi = require('hapi')
|
||||
const inert = require('inert')
|
||||
const vision = require('vision')
|
||||
const ejs = require('ejs')
|
||||
const pino = require('pino')()
|
||||
const hapiPino = require('hapi-pino')
|
||||
const routes = {
|
||||
index: require('./routes/index'),
|
||||
devStatic: require('./routes/dev-static')
|
||||
}
|
||||
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
const server = new hapi.Server()
|
||||
|
||||
server.connection({
|
||||
host: 'localhost',
|
||||
port: port
|
||||
})
|
||||
|
||||
const plugins = dev ? [{
|
||||
register: hapiPino,
|
||||
options: {instance: pino}
|
||||
}, vision, inert] : [{
|
||||
register: hapiPino,
|
||||
options: {instance: pino}
|
||||
}, vision]
|
||||
|
||||
server.register(plugins, start)
|
||||
|
||||
function start (err) {
|
||||
if (err) throw err
|
||||
server.views({
|
||||
engines: { ejs },
|
||||
relativeTo: __dirname,
|
||||
path: 'views'
|
||||
})
|
||||
|
||||
routes.index(server)
|
||||
|
||||
if (dev) routes.devStatic(server)
|
||||
|
||||
server.start((err) => {
|
||||
if (err) throw err
|
||||
server.log(`Server listening on port ${port}`)
|
||||
})
|
||||
}
|
||||
@ -1,20 +0,0 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ejs": "^2.5.6",
|
||||
"hapi": "^16.1.1",
|
||||
"hapi-pino": "^1.4.1",
|
||||
"inert": "^4.2.0",
|
||||
"pino": "^4.2.4",
|
||||
"vision": "^4.1.1"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = devStatic
|
||||
|
||||
function devStatic (server) {
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: '/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: 'public'
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = index
|
||||
|
||||
function index (server) {
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: '/',
|
||||
handler: function (request, reply) {
|
||||
const title = 'Hapi'
|
||||
request.logger.info(`rendering index view with ${title}`)
|
||||
reply.view('index', {title})
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title> <%= title %> </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> <%= title %> </h1>
|
||||
<p> Welcome to <%= title %> </p>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,34 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {join} = require('path')
|
||||
const Koa = require('koa')
|
||||
const serve = require('koa-static')
|
||||
const views = require('koa-views')
|
||||
const router = require('koa-router')()
|
||||
const pino = require('pino')()
|
||||
const logger = require('koa-pino-logger')({
|
||||
instance: pino
|
||||
})
|
||||
const index = require('./routes/index')
|
||||
|
||||
const app = new Koa()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.use(views(join(__dirname, 'views'), {
|
||||
extension: 'ejs'
|
||||
}))
|
||||
|
||||
app.use(logger)
|
||||
|
||||
if (dev) {
|
||||
app.use(serve(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
router.use('/', index.routes())
|
||||
|
||||
app.use(router.routes())
|
||||
|
||||
app.listen(port, () => {
|
||||
pino.info(`Server listening on port ${port}`)
|
||||
})
|
||||
@ -1,21 +0,0 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ejs": "^2.5.6",
|
||||
"koa": "^2.2.0",
|
||||
"koa-pino-logger": "^2.1.0",
|
||||
"koa-router": "^7.1.1",
|
||||
"koa-static": "^3.0.0",
|
||||
"koa-views": "^6.0.1",
|
||||
"pino": "^4.2.4"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const router = require('koa-router')()
|
||||
|
||||
router.get('/', async function (ctx, next) {
|
||||
await next()
|
||||
ctx.log.info(`rendering index view with ${ctx.state.title}`)
|
||||
await ctx.render('index')
|
||||
}, async (ctx) => ctx.state = {title: 'Koa'})
|
||||
|
||||
|
||||
// simplified:
|
||||
// router.get('/', async (ctx, next) => {
|
||||
// . ctx.log.info(`rendering index view with ${ctx.state.title}`)
|
||||
// await ctx.render('index', {title: 'Koa'})
|
||||
// })
|
||||
|
||||
module.exports = router
|
||||
|
||||
@ -1,10 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title> <%= title %> </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> <%= title %> </h1>
|
||||
<p> Welcome to <%= title %> </p>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,34 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const hapi = require('hapi')
|
||||
const inert = require('inert')
|
||||
const routes = {
|
||||
index: require('./routes/index'),
|
||||
devStatic: require('./routes/dev-static')
|
||||
}
|
||||
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
const server = new hapi.Server()
|
||||
|
||||
server.connection({
|
||||
host: 'localhost',
|
||||
port: port
|
||||
})
|
||||
|
||||
if (dev) server.register(inert, start)
|
||||
else start()
|
||||
|
||||
function start (err) {
|
||||
if (err) throw err
|
||||
|
||||
routes.index(server)
|
||||
|
||||
if (dev) routes.devStatic(server)
|
||||
|
||||
server.start((err) => {
|
||||
if (err) throw err
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = devStatic
|
||||
|
||||
function devStatic (server) {
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: '/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: 'public'
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = index
|
||||
|
||||
function index (server) {
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: '/',
|
||||
handler: function (request, reply) {
|
||||
const title = 'Hapi'
|
||||
reply(`
|
||||
<html>
|
||||
<head>
|
||||
<title> ${title} </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> ${title} </h1>
|
||||
<p> Welcome to ${title} </p>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const hapi = require('hapi')
|
||||
const inert = require('inert')
|
||||
const answer = require('./plugins/answer')
|
||||
const routes = {
|
||||
index: require('./routes/index'),
|
||||
devStatic: require('./routes/dev-static')
|
||||
}
|
||||
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
const server = new hapi.Server()
|
||||
|
||||
server.connection({
|
||||
host: 'localhost',
|
||||
port: port
|
||||
})
|
||||
|
||||
const plugins = dev ? [answer, inert] : [answer]
|
||||
|
||||
server.register(plugins, start)
|
||||
|
||||
function start (err) {
|
||||
if (err) throw err
|
||||
|
||||
routes.index(server)
|
||||
|
||||
if (dev) routes.devStatic(server)
|
||||
|
||||
server.start((err) => {
|
||||
if (err) throw err
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"hapi": "^16.1.1",
|
||||
"inert": "^4.2.0"
|
||||
}
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = answer
|
||||
|
||||
function answer (server, options, next) {
|
||||
server.ext('response', (request, reply) => {
|
||||
request.response.header('X-Answer', 42)
|
||||
reply.continue()
|
||||
})
|
||||
next()
|
||||
}
|
||||
|
||||
answer.attributes = {name: 'answer'}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = devStatic
|
||||
|
||||
function devStatic (server) {
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: '/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: 'public'
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = index
|
||||
|
||||
function index (server) {
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: '/',
|
||||
handler: function (request, reply) {
|
||||
const title = 'Hapi'
|
||||
reply(`
|
||||
<html>
|
||||
<head>
|
||||
<title> ${title} </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> ${title} </h1>
|
||||
<p> Welcome to ${title} </p>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,46 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const hapi = require('hapi')
|
||||
const inert = require('inert')
|
||||
const routes = {
|
||||
index: require('./routes/index'),
|
||||
devStatic: require('./routes/dev-static')
|
||||
}
|
||||
|
||||
const devPort = process.env.DEV_PORT || 3000
|
||||
const prodPort = process.env.PORT || 8080
|
||||
|
||||
const server = new hapi.Server()
|
||||
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
|
||||
if (dev) server.connection({
|
||||
host: 'localhost',
|
||||
port: devPort,
|
||||
labels: ['dev', 'staging']
|
||||
})
|
||||
|
||||
if (!dev) server.connection({
|
||||
host: '0.0.0.0',
|
||||
port: prodPort,
|
||||
labels: 'prod'
|
||||
})
|
||||
|
||||
server.register({
|
||||
register: inert,
|
||||
select: ['dev', 'staging']
|
||||
}, start)
|
||||
|
||||
function start (err) {
|
||||
if (err) throw err
|
||||
|
||||
routes.index(server)
|
||||
|
||||
routes.devStatic(server)
|
||||
|
||||
server.start((err) => {
|
||||
if (err) throw err
|
||||
console.log(`Dev/Staging server listening on port ${devPort}`)
|
||||
console.log(`Prod server listening on port ${prodPort}`)
|
||||
})
|
||||
}
|
||||
@ -1,16 +0,0 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"hapi": "^16.1.1",
|
||||
"inert": "^4.2.0"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = devStatic
|
||||
|
||||
function devStatic (server) {
|
||||
server.select(['dev', 'staging']).route({
|
||||
method: 'GET',
|
||||
path: '/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: 'public'
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = index
|
||||
|
||||
function index (server) {
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: '/',
|
||||
handler: function (request, reply) {
|
||||
const title = 'Hapi'
|
||||
reply(`
|
||||
<html>
|
||||
<head>
|
||||
<title> ${title} </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> ${title} </h1>
|
||||
<p> Welcome to ${title} </p>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
'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}`)
|
||||
})
|
||||
@ -1,17 +0,0 @@
|
||||
{
|
||||
"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"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
'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
|
||||
@ -1,23 +0,0 @@
|
||||
'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}`)
|
||||
})
|
||||
@ -1,17 +0,0 @@
|
||||
{
|
||||
"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"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
'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
|
||||
@ -1,27 +0,0 @@
|
||||
'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}`)
|
||||
})
|
||||
@ -1,10 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = answer
|
||||
|
||||
function answer () {
|
||||
return async (ctx, next) => {
|
||||
ctx.set('X-Answer', 42)
|
||||
await next()
|
||||
}
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
{
|
||||
"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"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
'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
|
||||
@ -1,19 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {join} = require('path')
|
||||
const express = require('express')
|
||||
const index = require('./routes/index')
|
||||
|
||||
const app = express()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
if (dev) {
|
||||
app.use(express.static(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
app.use('/', index)
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {Router} = require('express')
|
||||
const router = Router()
|
||||
|
||||
router.get('/', function (req, res) {
|
||||
const title = 'Express'
|
||||
res.send(`
|
||||
<html>
|
||||
<head>
|
||||
<title> ${title} </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> ${title} </h1>
|
||||
<p> Welcome to ${title} </p>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@ -1,26 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const express = require('express')
|
||||
const {join} = require('path')
|
||||
const index = require('./routes/index')
|
||||
const answer = require('./middleware/answer')
|
||||
|
||||
const app = express()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.use(answer())
|
||||
|
||||
if (dev) {
|
||||
app.use(express.static(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
app.use('/', index)
|
||||
|
||||
app.use((req, res, next) => {
|
||||
next(Object.assign(Error('Not Found'), {status: 404}))
|
||||
})
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
@ -1,10 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = answer
|
||||
|
||||
function answer () {
|
||||
return (req, res, next) => {
|
||||
res.setHeader('X-Answer', 42)
|
||||
next()
|
||||
}
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"express": "^4.15.2"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,20 +0,0 @@
|
||||
const {Router} = require('express')
|
||||
const router = Router()
|
||||
|
||||
router.get('/', function(req, res, next) {
|
||||
const title = 'Express'
|
||||
res.send(`
|
||||
<html>
|
||||
<head>
|
||||
<title> ${title} </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> ${title} </h1>
|
||||
<p> Welcome to ${title} </p>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@ -1,22 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {join} = require('path')
|
||||
const express = require('express')
|
||||
const bodyParser = require('body-parser')
|
||||
const index = require('./routes/index')
|
||||
|
||||
const app = express()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.use(bodyParser.urlencoded({extended: false}))
|
||||
|
||||
if (dev) {
|
||||
app.use(express.static(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
app.use('/', index)
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
@ -1,16 +0,0 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.17.1",
|
||||
"express": "^4.15.2"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,35 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {Router} = require('express')
|
||||
const router = Router()
|
||||
|
||||
router.get('/:name?', function (req, res) {
|
||||
const title = 'Express'
|
||||
// CAUTION: never place user input
|
||||
// directly into HTML output in production
|
||||
// without sanitizing it first. Otherwise, we make
|
||||
// ourselves vulnerable to XSS attacks.
|
||||
// See Chapter 8 Dealing with Security for details
|
||||
const name = req.params.name
|
||||
res.send(`
|
||||
<html>
|
||||
<head>
|
||||
<title> ${title} </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> ${title} </h1>
|
||||
<p> Welcome to ${title}${name ? `, ${name}.` : ''} </p>
|
||||
<form method=POST action=data>
|
||||
Name: <input name=name> <input type=submit>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
})
|
||||
|
||||
router.post('/data', function (req, res) {
|
||||
res.redirect(`/${req.body.name}`)
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@ -1,40 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {join} = require('path')
|
||||
const express = require('express')
|
||||
const pino = require('pino')()
|
||||
const logger = require('express-pino-logger')({
|
||||
instance: pino
|
||||
})
|
||||
const session = require('express-session')
|
||||
const bodyParser = require('body-parser')
|
||||
const index = require('./routes/index')
|
||||
const auth = require('./routes/auth')
|
||||
|
||||
const app = express()
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
app.set('views', join(__dirname, 'views'))
|
||||
app.set('view engine', 'ejs')
|
||||
if (!dev) app.set('trust proxy', 1)
|
||||
|
||||
app.use(logger)
|
||||
app.use(session({
|
||||
secret: 'I like pies',
|
||||
resave: false,
|
||||
saveUninitialized: false,
|
||||
cookie: {secure: !dev}
|
||||
}))
|
||||
app.use(bodyParser.urlencoded({extended: false}))
|
||||
|
||||
if (dev) {
|
||||
app.use(express.static(join(__dirname, 'public')))
|
||||
}
|
||||
|
||||
app.use('/', index)
|
||||
app.use('/auth', auth)
|
||||
|
||||
app.listen(port, () => {
|
||||
pino.info(`Server listening on port ${port}`)
|
||||
})
|
||||
@ -1,20 +0,0 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.17.1",
|
||||
"ejs": "^2.5.6",
|
||||
"express": "^4.15.2",
|
||||
"express-pino-logger": "^2.0.0",
|
||||
"express-session": "^1.15.2",
|
||||
"pino": "^4.2.4"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,34 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const { Router } = require('express')
|
||||
const router = Router()
|
||||
|
||||
router.get('/login', function (req, res, next) {
|
||||
res.render('login', {fail: false})
|
||||
next()
|
||||
})
|
||||
|
||||
router.post('/login', function (req, res, next) {
|
||||
if (req.session.user) {
|
||||
res.redirect('/')
|
||||
next()
|
||||
return
|
||||
}
|
||||
if (req.body.un === 'dave' && req.body.pw === 'ncb') {
|
||||
req.session.user = {name: req.body.un}
|
||||
res.redirect('/')
|
||||
next()
|
||||
return
|
||||
}
|
||||
|
||||
res.render('login', {fail: true})
|
||||
|
||||
next()
|
||||
})
|
||||
|
||||
router.get('/logout', function (req, res, next) {
|
||||
req.session.user = null
|
||||
res.redirect('/')
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@ -1,13 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const {Router} = require('express')
|
||||
const router = Router()
|
||||
|
||||
router.get('/', function (req, res) {
|
||||
const title = 'Express'
|
||||
req.log.info(`rendering index view with ${title}`)
|
||||
const user = req.session.user
|
||||
res.render('index', {title, user})
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
@ -1,16 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title> <%= title %> </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> <%= title %> </h1>
|
||||
<p> Welcome to <%= title %> </p>
|
||||
<% if (user) { %>
|
||||
<p> Hi <%= user.name %>! </p>
|
||||
<p> <a href=/auth/logout> Logout </a> </p>
|
||||
<% } else { %>
|
||||
<p> <a href=/auth/login> Login </a> </p>
|
||||
<% } %>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,17 +0,0 @@
|
||||
<html>
|
||||
<head>
|
||||
<title> Login </title>
|
||||
<link rel="stylesheet" href="../styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> Login </h1>
|
||||
<% if (fail) { %>
|
||||
<h2> Try Again </h2>
|
||||
<% } %>
|
||||
<form method=post action=login>
|
||||
User: <input name=un> <br>
|
||||
Pass: <input type=password name=pw> <br>
|
||||
<input type=submit value="login">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
@ -1,69 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const hapi = require('hapi')
|
||||
const inert = require('inert')
|
||||
const vision = require('vision')
|
||||
const ejs = require('ejs')
|
||||
const pino = require('pino')()
|
||||
const hapiPino = require('hapi-pino')
|
||||
const yar = require('yar')
|
||||
const routes = {
|
||||
index: require('./routes/index'),
|
||||
auth: require('./routes/auth'),
|
||||
devStatic: require('./routes/dev-static')
|
||||
}
|
||||
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const port = process.env.PORT || 3000
|
||||
|
||||
const server = new hapi.Server()
|
||||
|
||||
server.connection({
|
||||
host: '127.0.0.1',
|
||||
port: port
|
||||
})
|
||||
|
||||
const plugins = dev ? [{
|
||||
register: hapiPino,
|
||||
options: {instance: pino}
|
||||
}, {
|
||||
register: yar,
|
||||
options: {
|
||||
cookieOptions: {
|
||||
password: 'I really really really like pies',
|
||||
isSecure: false
|
||||
}
|
||||
}
|
||||
}, vision, inert] : [{
|
||||
register: hapiPino,
|
||||
options: {instance: pino}
|
||||
}, {
|
||||
register: yar,
|
||||
options: {
|
||||
cookieOptions: {
|
||||
password: 'something more secure than a bit about pies',
|
||||
isSecure: true
|
||||
}
|
||||
}
|
||||
}, vision]
|
||||
|
||||
server.register(plugins, start)
|
||||
|
||||
function start (err) {
|
||||
if (err) throw err
|
||||
server.views({
|
||||
engines: { ejs },
|
||||
relativeTo: __dirname,
|
||||
path: 'views'
|
||||
})
|
||||
|
||||
routes.index(server)
|
||||
routes.auth(server)
|
||||
|
||||
if (dev) routes.devStatic(server)
|
||||
|
||||
server.start((err) => {
|
||||
if (err) throw err
|
||||
server.log(`Server listening on port ${port}`)
|
||||
})
|
||||
}
|
||||
@ -1,21 +0,0 @@
|
||||
{
|
||||
"name": "app",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"ejs": "^2.5.6",
|
||||
"hapi": "^16.1.1",
|
||||
"hapi-pino": "^1.4.1",
|
||||
"inert": "^4.2.0",
|
||||
"pino": "^4.2.4",
|
||||
"vision": "^4.1.1",
|
||||
"yar": "^8.1.2"
|
||||
}
|
||||
}
|
||||
@ -1,4 +0,0 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = auth
|
||||
|
||||
function auth (server) {
|
||||
|
||||
server.route({
|
||||
method: ['GET', 'POST'],
|
||||
path: '/auth/login',
|
||||
handler: function (request, reply) {
|
||||
if (request.auth.isAuthenticated) {
|
||||
reply.redirect('/');
|
||||
return
|
||||
}
|
||||
|
||||
if (request.method === 'get') {
|
||||
reply.view('login', {fail: false})
|
||||
return
|
||||
}
|
||||
|
||||
if (request.method === 'post') {
|
||||
if (request.payload.un === 'dave' && request.payload.pw === 'ncb') {
|
||||
request.yar.set('user', {name: request.payload.un})
|
||||
reply.redirect('/')
|
||||
} else {
|
||||
reply.view('login', {fail: true})
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: '/auth/logout',
|
||||
handler: function (request, reply) {
|
||||
request.yar.reset()
|
||||
reply.redirect('/')
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = devStatic
|
||||
|
||||
function devStatic (server) {
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: '/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: 'public'
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user