Last active
August 17, 2019 13:56
-
-
Save sivasankars/0494734cbf6bd70646403c348b5b87d4 to your computer and use it in GitHub Desktop.
Creating Web Server With Routes Using Hapi.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Hapi = require('@hapi/hapi'); | |
const users = [ | |
{ id: 1, name: 'Siva' }, | |
{ id: 2, name: 'Sivasankar' }, | |
{ id: 3, name: 'Niralar' } | |
] | |
const server = Hapi.server({ | |
port: 3000, | |
host: 'localhost' | |
}); | |
server.route({ | |
method: 'GET', | |
path: '/', | |
handler: (request, h) => { | |
return 'Hello World!'; | |
} | |
}); | |
server.route({ | |
method: 'POST', | |
path: '/user', | |
handler: (request, h) => { | |
if (request.payload && request.payload.name) { | |
let checkUser = users.find(user => user.name === request.payload.name); | |
if (checkUser) { | |
return h.response('User already exists').code(422); | |
} | |
return h.response('User added successfully').code(200); | |
} else { | |
return h.response('Invalid user').code(422); | |
} | |
} | |
}); | |
server.route({ | |
method: 'GET', | |
path: '/user/{id}', | |
handler: (request, h) => { | |
let user = users.find(user => user.id === parseInt(request.params.id)); | |
if (!user) { | |
return h.response('User not exists').code(422); | |
} | |
return user; | |
} | |
}); | |
server.route({ | |
method: 'GET', | |
path: '/user/list', | |
handler: (request, h) => { | |
return users; | |
} | |
}); | |
const init = async () => { | |
await server.start(); | |
console.log('Server running on %ss', server.info.uri); | |
}; | |
process.on('unhandledRejection', (err) => { | |
console.log(err); | |
process.exit(1); | |
}); | |
init(); | |
module.exports = server; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment