Getting Started with Hapi Server.
Here, you will create a new hapi server. First create a new project folder someproject
on your machine so that it contains the necessary files.
Open your terminal (linux hit: Ctrl + Alt + T
) and run the following command. Running the command will create a new folder someproject
in the current directory and change the current directory to someproject
directory.
$ mkdir someproject && cd someproject
To install the dependency of the project, we will use NPM (node package manager) so that the dependency can be installed easily.
Now, we need to run npm init
. If you run this command, you will be asked some information. We can keep the default information. If you want, you can change it from the package.json.
$ npm init -y # Generate it without having it ask any questions
Now the package.json has been added to your project folder, you can now add project dependency.
I added hapiJs as my project dependency by running the below command.
$ npm install @hapi/hapi -S # saving hapi as a dependency by adding -S to npm i
Basic hapi server
Below is a very basic server shown in the code snippet. First, you need to require a hapi module and start a new Hapi.Server()
. After that, you have to say host
and port
as server options of the server.
Here, I have written the code to run the server using IIFE.
// server.js
const Hapi = require('@hapi/hapi');
(async () => {
// start your server
try {
// create a server with a host and port
const server = new Hapi.Server({
host: 'localhost',
port: 3000
});
await server.start();
console.log('Server running at: ', server.info.uri);
}catch (err) {
console.error(err);
}
})(); // IIFE
In fact, it is a very basic hapi server. Create a file named server.js and write the code in that file.
Below is my project folder structure.
# file stracture
|-- node_modules
|-- package.json
|-- server.js
Hello World Route
After the server runs, you need a route where if you request, you will get the text "Hello World"
as a response.
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return 'Hello World!';
}
});
If you add a route to the server.js
file, the code will look like the following code.
// server.js
const Hapi = require('@hapi/hapi');
(async () => {
// start your server
try {
// create a server with a host and port
const server = new Hapi.Server({
host: 'localhost',
port: 3000
});
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
// business logic
return 'Hello World!';
}
});
await server.start();
console.log('Server running at: ', server.info.uri);
}catch (err) {
console.error(err);
}
})(); // IIFE
Now, after saving and running the code from server.js
file, if you request to localhost:3000
, the response will be Hello World!
.
Here the method property can be any valid HTTP method, the method can also be an array of methods (e.g: ['GET', 'POST']
). Define the path property and request the endpoint URL of this route. And you have to write the main business logic of your route in this handler. The handler must return someting, otherwise it will give an error.
Run the server
Go to the project folder, open the terminal and run the following command, the server will start.
$ node server.js
Awesome ๐, you have created your first hapi server ๐!
Subscribe to my newsletter
Read articles from Prosen Ghosh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by