Hono Tutorial Pt. 1

Kiran Chauhan
3 min readJul 19, 2024

--

This is the first article in a series where we are going to build a simple back-end application in Hono in Node.js environment.

Create a new folder with the name hono-app.

mkdir hono-app

Go inside the created hono-app folder

cd hono-app

Create a package.json file using npm with default values (-y).

npm init -y

Let’s install the hono package.

npm i hono

In order to use Hono in the Node.js environment, we also need to the @hono/node-server package.

npm i @hono/node-server

Finally, let’s install nodemon to ease the development. We should install this as a development dependency.

npm i -D nodemon

Create a new file with the name app.js.

touch app.js

Open this file (or hono-app folder) in your favorite text-editor/IDE and write the following code.

import { Hono } from 'hono';

const app = new Hono();

We are using the latest import…from syntax in our Node.js application! Here, we imported the Hono and then created an instance of the Hono() using a new keyword and saved it as an app.

Let’s now define a root route and return a simple Hello! text as response (don’t forget to return the response).

import { Hono } from 'hono';

const app = new Hono();

app.get('/', (c) => {
return c.text('Hello!');
});

Here, c is context and .text() returns a plain text response Hello!

Now, to run the application on a given port, we need to use the @hono/node-server package. Import serve from @hono/node-server package.

import { serve } from '@hono/node-server';
import { Hono } from 'hono';

const app = new Hono();

app.get('/', (c) => {
return c.text('Hello!');
});

Use this serve method as follows.

import { serve } from '@hono/node-server';
import { Hono } from 'hono';

const app = new Hono();

app.get('/', (c) => {
return c.text('Hello!');
});

serve(app, (info) => {
console.log(`Listening on http://localhost:${info.port}`);
});

Created Hono instance should be the first argument of the serve() function and second argument is the callback function with info as the parameter that has a port attribute. If you don’t define the port value, the default is 3000.

Before we go further and run this application, we need to do some adjustment in the package.json file. As we are using the latest import…from syntax, we need to define type as a module. Also, update the main entry to app.js file.

{
"name": "hono-app",
"version": "1.0.0",
"description": "",
"main": "app.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@hono/node-server": "^1.12.0",
"hono": "^4.5.0"
},
"devDependencies": {
"nodemon": "^3.1.4"
}
}

Finally, let’s add two scripts dev and start that run the app.js file with nodemon and node respectively.

{
"name": "hono-app",
"version": "1.0.0",
"description": "",
"main": "app.js",
"type": "module",
"scripts": {
"dev": "nodemon app.js",
"start": "node app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@hono/node-server": "^1.12.0",
"hono": "^4.5.0"
},
"devDependencies": {
"nodemon": "^3.1.4"
}
}

With these changes, we are now ready to run the application. Run the following command in the terminal.

npm run dev
Listening on http://localhost:3000

Let’s open this http://localhost:3000 in browser and we should see Hello! as the plain text. If that is the case, congratulations! You have just made a first Hono application in Node.js.

Take a break and read the next article in this series at this link.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Kiran Chauhan
Kiran Chauhan

Written by Kiran Chauhan

I design software with and for people.

No responses yet

Write a response