Full-stack Walkthrough

Add a server-rendered notes page with a loader and a form that saves a note.

Create the app

Start with the Full-stack template. It runs directly on Node and uses h() to build pages. The scaffold declares the framework, server and build dependencies in package.json. This example adds no dependencies.

Terminal
npm create what@latest my-notes -- --template=fullstack --yes
cd my-notes
npm install

Add the notes store and action

Start with a note in memory so the first server response already has data. Notes are shared by visitors to this process and reset when it restarts.

src/notes.js
const notes = [{ id: 1, text: 'Hello from the server' }];

export function listNotes() {
  return notes;
}

export function addNote(text) {
  notes.push({ id: notes.length + 1, text });
}

The page imports the action declaration. What runs the callback on the server, where it imports src/notes.js. After a valid submission, redirect back to the route so its loader returns the updated notes.

src/components/note-actions.js
import { getResponse } from 'what-framework';
import { action } from 'what-framework/server';

export const saveNote = action(async ({ text }) => {
  const note = String(text ?? '').trim();
  if (note) {
    const { addNote } = await import('../notes.js');
    addNote(note);
  }
  getResponse().redirect('/notes');
}, { id: 'saveNote' });

Render the page and form

The loader reads the notes before rendering. Use mode: 'server' so each request runs the loader. Pass its CSRF token to Form to authorize the submission. This example sets enhance: false so the browser submits a normal HTML form and follows the server’s redirect, with or without JavaScript.

src/pages/notes.js
import { h, useLoaderData } from 'what-framework';
import { Form } from 'what-framework/server';
import { saveNote } from '../components/note-actions.js';

export const page = { mode: 'server' };

export async function loader({ csrfToken }) {
  const { listNotes } = await import('../notes.js');
  return { notes: listNotes(), csrfToken };
}

export default function Notes() {
  const { notes, csrfToken } = useLoaderData();
  return h('main', { class: 'container' },
    h('h1', {}, 'Notes'),
    h('ul', {}, notes.map(({ id, text }) => h('li', { key: id }, text))),
    h(Form, { action: saveNote, csrfToken, enhance: false },
      h('label', {}, 'Note ', h('input', { name: 'text', required: true })),
      h('button', { type: 'submit' }, 'Save note'),
    ),
  );
}

Register the route

In src/routes.js, add this import alongside the existing page imports. Importing the page also registers its action on the server.

src/routes.js (import)
import Notes, { loader as notesLoader, page as notesPage } from './pages/notes.js';

Add this entry inside the existing routes array:

src/routes.js (route)
{ path: '/notes', component: Notes, loader: notesLoader, page: notesPage, mode: notesPage.mode },

The notes page has no client interaction beyond submitting the HTML form, so its page component only needs the server route. At the bottom of src/entry-client.js, change the existing document.querySelectorAll('form[data-action]') selector to document.querySelectorAll('form[data-action="createPost"]'). That listener belongs to the scaffold’s blog form.

Try it in the browser

Terminal
npm run dev

Open http://localhost:3000/notes. The first note is in the page source, so it displays even with JavaScript disabled. Enter a note and select Save note. The server saves it, redirects to /notes and renders both notes. Try a second submission with JavaScript disabled to see the same result through a normal form POST.

Continue with Data Loading and Server Actions for loader inputs, validation, and error handling.