Skip to main content

Command Palette

Search for a command to run...

API With Hono For You Javascript Project

By Ahmad Afan Shobari

Updated
2 min read
API With Hono For You Javascript Project
A
I'm a Programmer

Assalamualaikum, Hello everyone i hope you good today, so i’ll explain you about Hono (similiar with next.js).

What is Hono?

Hono is framework to many language programming, is simple, fast, and easy, hono in web dev it is mean backend developer.

backend developer is programmer who menages data and server for website

How To Use Hono In Javascript

First, initialization npm in your project

npm init

then change type on package.json to “module”

next install hono with npm, yarn, or other ways.

npm install hono @hono/node-server

then create file server.js and this code

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

const app = new Hono();

app.get("/", (c) => {
    return c.html(`
        <h1>Hallo Hono</h1>
        <p>Rute yang tersedia:</p>
        <ul>
        <li><a href="/text">/text</a> - Menampilka Text biasa</li>

        <li><a href="/json">/json</a> - Menampilkan Output JSON</li>
        </ul>`);
});

// Rute untuk Text
app.get("/text", (c) => c.text("ini adalah respons text biasa"));

// Rute untuk JSON
app.get("/json", (c) => c.json({data : "Hello Santri.", status: "ok"}));

serve({fetch: app.fetch, port:3100}, (info) => {
    console.log(`server berjalan di http://localhost:${info.port}`);
});

so now you can running the code

node server.js

so this is result

How Try API

you can use postman, echo api, etc, for example i use echo api here.

and success…..

Closing

thanks for watching, see you next video…..