Creating Landing Page
Introduction
In the previous lesson, you learned how to use Codex to set up an Express project with TypeScript and create a dice - rolling api endpoint. Now, let's build on that foundation by adding a user - friendly landing page to your Express app. In this lesson, you will use Codex to create a new route handler for the home page, serve an HTML template, build a minimal interactive frontend, and enhance the user experience with modern UI features. By the end, you'll have a polished landing page that lets users roll dice and see results instantly.
Creating the Landing Page Route Handler
Let's start by adding a new route handler for the landing page and serving an HTML page from the root path (/). This is a common pattern in Express: you create a route handler function, then serve HTML content to the browser.
First, you can ask Codex to create a new route in your Express app. Here's a prompt you might use:
This prompt tells Codex exactly what you want: a route for the home page that serves a static HTML file. Codex will generate the route handler, import the necessary modules like path, and use res.sendFile().
Next, you need to configure Express to serve static files from a public directory. You can instruct Codex with a prompt like:
This tells Codex to add the static file middleware to your Express app, which will allow you to serve CSS, JavaScript, and other assets alongside your HTML. Make sure this middleware is added before your routes so that static files are served first.
By breaking your instructions into clear, single - purpose prompts, you help Codex generate accurate and maintainable code. Your existing /roll/ api endpoint will remain functional, and now you'll have a home page route as well.
Setting Up HTML File Structure
Before building the template, let's set up the proper file structure for serving HTML in Express. Unlike Django's template system, Express doesn't enforce a specific directory structure, so we'll use a straightforward approach.
You can ask Codex to help set this up:
This creates a clear separation between your server code (TypeScript files) and your frontend assets (HTML, CSS, JavaScript). The public directory is a common convention in Express apps for serving static content directly to the browser.
Now your project structure should look something like this:
This structure keeps your code organized and makes it easy to add more static assets like stylesheets or client - side JavaScript files later.
Building a Minimal Interactive Template
Now that you have a route handler and file structure, let's create the HTML for your landing page. The goal is to let users pick how many dice to roll and how many sides each die has, then see the results.
Start with a prompt like this:
Let's break down what this prompt does:
- It specifies the file path and the main elements you want in the HTML.
- It describes the form controls: two range sliders for
countandsides, each with a live - updating badge. - It asks for quick - select chips for common values, making it easy for users to pick popular options.
- It requests a submit button that uses JavaScript's
fetchto call your/roll/api and display the results. - It includes a loading state, an error area, and a results area with
aria - livefor accessibility. - It specifies that JavaScript should be inline, keeping everything in one file for simplicity.
Since we're using plain HTML (not a template engine like EJS or Pug), all the HTML markup, CSS, and JavaScript will live in a single index.html file. This keeps things simple and doesn't require any additional dependencies.
By being specific about the ui elements and their behaviors, you help Codex generate an HTML page that is both functional and user - friendly.
Improving User Experience with Codex
Once you have the basic HTML template, you can use Codex to further enhance the user experience. Here are some focused prompts and explanations for each improvement:
1. Live - updating value badges for sliders
You want the number next to each slider to update as the user moves it. You can prompt Codex like this:
This tells Codex to add input event listeners to the range sliders and update the badge text in real time using dom manipulation.
2. Quick - select chips for common values
To make it easy for users to pick common dice counts or sides, you can use chips. Here's a prompt:
This ensures that when a user clicks a chip, the slider position and badge update to match, providing a seamless experience.
3. Loading state and inline error display
To give feedback while the dice are rolling and to handle errors, you can use:
This prompt helps Codex add logic to disable the button during the fetch, show a Rolling… message, and display errors if something goes wrong (like network issues or invalid responses).
4. Accessibility with aria - live
To make sure screen readers announce the results, you can say:
This small addition makes your app more accessible to all users, ensuring that screen reader users get immediate feedback when the dice results appear.
By giving Codex clear, focused prompts for each enhancement, you can build a modern, interactive, and accessible landing page step by step. Each improvement adds polish and makes your app more professional.
Summary and What's Next
In this lesson, you learned how to use Codex to add a landing page to your Express app, create a route handler for the home page, set up static file serving, build a minimal interactive HTML page, and enhance the user experience with live badges, quick - select chips, loading states, error handling, and aria - live accessibility features. These skills will help you create user - friendly web applications quickly and efficiently.
In the next lesson, you'll extend your api to support dice notation (like 2d6 + 3) and add input validation with proper error handling using TypeScript types and Express middleware.
