Skip to content

TypeScript

This guide will show how to use hop in a TypeScript backend.

We start by writing a simple hop file.

hop
<page-index {name: String}>
  Hello {name}!
</page-index>

Now we compile the hop code to native TypeScript code.

bash
hop compile

Now the generated TypeScript code is available in the folder ./frontend so we can import it in the backend.

Now, let's define a simple backend using Express.

typescript
import express from 'express';
import * as frontend from './frontend';

const app = express();

app.get('/', (req, res) => {
    res.send(frontend.pageIndex({
        name: "Tobi"
    }));
});

app.listen(8080, () => {
    console.log('Server running on port 8080');
});

Now hop is integrated into the backend, and we can run hop dev to start the server and get hot-reloading.