Skip to content

Instantly share code, notes, and snippets.

@thephilip
Created May 3, 2017 17:16
Show Gist options
  • Save thephilip/ab2db0528c50b2eb49c15d533090cbc1 to your computer and use it in GitHub Desktop.
Save thephilip/ab2db0528c50b2eb49c15d533090cbc1 to your computer and use it in GitHub Desktop.
// Express.js
const express = require('express')
const app = express()
const router = express.Router()
router.route('/objects').get((req, res, next) => {
res.send('GET received for objects')
}).post((req, res, next) => {
res.send('POST received for objects')
})
router.route('/objects/:id').get((req, res, next) => {
res.send('GET received for object with ID of ' + req.params.id)
})
app.use('/api', router)
app.get('/', (req, res) => {
res.send('Howdy!')
})
const server = app.listen(3000, () => {
console.log('Started express server on port 3000.')
})
// Hapi.js
const Hapi = require('hapi')
const server = new Hapi.Server()
server.connection({ host: 'localhost', port: 3000 })
const buildApiRoute = uri => uri.indexOf('/') !== 0 ? uri = '/' + uri : 'api' + uri
server.route([
{
method: 'GET',
path: buildApiRoute('objects'),
handler: (request, reply) => reply('GET received for objects')
},
{
method: 'POST',
path: buildApiRoute('objects'),
handler: (request, reply) => reply('POST received for objects')
},
{
method: 'GET',
path: buildApiRoute('objects/{id}'),
handler: (request, reply) => reply('GET received for object with ID of ' + request.params.id)
},
{
method: 'GET',
path: '/',
handler: (request, reply) => reply('Howdy!')
}
])
server.start(() => console.log('Started Hapi server on port 3000.'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment