Code files
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
'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}`)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"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",
|
||||
"inert": "^4.2.0",
|
||||
"vision": "^4.1.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
body {
|
||||
padding: 50px;
|
||||
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = devStatic
|
||||
|
||||
function devStatic (server) {
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: '/{param*}',
|
||||
handler: {
|
||||
directory: {
|
||||
path: 'public'
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = index
|
||||
|
||||
function index (server) {
|
||||
server.route({
|
||||
method: 'GET',
|
||||
path: '/',
|
||||
handler: function (request, reply) {
|
||||
const title = 'Hapi'
|
||||
reply.view('index', {title})
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<html>
|
||||
<head>
|
||||
<title> <%= title %> </title>
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
</head>
|
||||
<body>
|
||||
<h1> <%= title %> </h1>
|
||||
<p> Welcome to <%= title %> </p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user