Hapi.js is a small Node framework for developing back end web apps.
In this article, we’ll look at how to create back end apps with Hapi.js.
Getting Started
We can start by creating a project folder, go into it, and run:
npm install @hapi/hapi
to install the package.
Creating a Server
Once we installed the package, we can create a simple back end app by writing:
const Hapi = require('@hapi/hapi');
const init = async () => {
const server = Hapi.server({
port: 3000,
host: '0.0.0.0'
});
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return 'Hello World!';
}
});
await server.start();
console.log('Server running on %s', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
We require the hapi
package.
Then we run the Hapi.server
method to create the server.
host
is set to '0.0.0.0'
to listen for request from all IP addresses.
And then to create a route, we call the server.route
method.
The method
is the request method.
handler
is the request handler function.
Then we call server.start
to start the server.
Next, we add the unhandledRejection
error handler to exit the app gracefully when the app crashes.
Authentication
We can add authentication to our app with a few lines of code.
For instance, we can write:
const Bcrypt = require('bcrypt');
const Hapi = require('@hapi/hapi');
const users = {
john: {
username: 'john',
password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm',
name: 'John Doe',
id: '1'
}
};
const validate = async (request, username, password) => {
const user = users[username];
if (!user) {
return { credentials: null, isValid: false };
}
const isValid = await Bcrypt.compare(password, user.password);
const credentials = { id: user.id, name: user.name };
return { isValid, credentials };
};
const start = async () => {
const server = Hapi.server({ port: 4000 });
await server.register(require('@hapi/basic'));
server.auth.strategy('simple', 'basic', { validate });
server.route({
method: 'GET',
path: '/',
options: {
auth: 'simple'
},
handler(request, h) {
return 'welcome';
}
});
await server.start();
console.log('server running at: ' + server.info.uri);
};
start();
to add simple authentication with the bcrypt
and the hapi-basic
modules.
The users
object has the user data.
The validate
function gets the request data from the request
parameter.
We get the user by the username
.
Then we call Bcrypt.compare
to compare the password we entered with the hash we stored in the users
object’s password
property.
Then we have:
server.auth.strategy('simple', 'basic', { validate });
to add the basic authentication strategy.
And we define our route with the server.route
.
The options.auth
property is set to the name of the authentication strategy we added.
Now when we go to the /
route, we see the login dialog box.
And when we enter john
for username and secret
for password, we see the welcome
response.
Conclusion
We can create a simple app with authentication with Hapi.