Skip to content

Instantly share code, notes, and snippets.

@caesaneer
Last active April 11, 2018 02:28
Show Gist options
  • Save caesaneer/d65f346a79490831cf8c4167fafe2fe0 to your computer and use it in GitHub Desktop.
Save caesaneer/d65f346a79490831cf8c4167fafe2fe0 to your computer and use it in GitHub Desktop.
React-hapi-example
// ════════════════════════════════════════════════════════════════════════════════════════════════╡
const Hapi = require('hapi')
const Inert = require('inert')
const chalk = require('chalk')
const path = require('path')
// ════════════════════════════════════════════════════════════════════════════════════════════════╡
// Set path to production build
const relativeTo = path.join(__dirname, '../build')
// Create server
const server = new Hapi.Server({
port: 8282,
host: 'localhost',
routes: {
files: {
relativeTo,
},
},
})
async function startServer() {
try {
try {
// Register Inert plugin
await server.register([
{
plugin: Inert,
},
])
} catch (e) {
throw new Error(`Unable to register Hapi plugins: WHY: ${e.message}`)
}
// Index route
server.route({
method: 'GET',
path: '/{param*}',
handler: {
directory: {
path: '.',
redirectToSlash: true,
index: true,
},
},
})
// Start server
try {
await server.start()
} catch (e) {
throw new Error(`Unable to start Hapi server: WHY: ${e.message}`)
}
console.log(chalk.blueBright('┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓'))
console.log(chalk.blueBright(`┃ REACT PRODUCTION SERVER: http://localhost:${server.info.port} ┃`))
console.log(chalk.blueBright('┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛'))
}
catch (e) {
console.log(chalk.redBright(`HAPI SERVER: The following errors occurred: ${e}`))
process.exit(1)
}
}
startServer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment