🎓 A1Learner
🔍
0 of 50 lessons completed

Capstone: A Full-Stack Notes App

The capstone is a full notes app built from every part of the course. It stores notes in MongoDB, serves them through an API, and shows them in React.

Notes in state

The seed notes start in state for the first render.

const [notes, setNotes] = useState([
  { title: 'Buy milk', done: false },
  { title: 'Ship MERN', done: true },
  { title: 'Read a book', done: false }
]);

Listing the notes

The list shows one item per note.

<ul>
  {notes.map(note => <li key={note.title}>{note.title}</li>)}
</ul>

Adding a note

The form posts the new note to the API.

await fetch(base + '/api/notes', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ title, done: false })
});

Example

import mongoose from 'mongoose';
import express from 'express';

await mongoose.connect('mongodb://localhost:27017/notes');

const noteSchema = new mongoose.Schema({ title: String, done: Boolean });
const Note = mongoose.model('Note', noteSchema);
await Note.create([
  { title: 'Buy milk', done: false },
  { title: 'Ship MERN', done: true },
  { title: 'Read a book', done: false }
]);

const app = express();
app.use(express.json());
app.get('/api/notes', async (req, res) => {
  const notes = await Note.find().sort({ title: 1 });
  res.json(notes.map((n) => ({ title: n.title, done: n.done })));
});
app.post('/api/notes', async (req, res) => {
  const note = await Note.create({ title: req.body.title, done: false });
  res.status(201).json({ title: note.title, done: note.done });
});

const server = app.listen(0, async () => {
  const base = 'http://localhost:' + server.address().port;
  const first = await (await fetch(base + '/api/notes')).json();
  console.log(JSON.stringify(first));
  await fetch(base + '/api/notes', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ title: 'Water the plants' })
  });
  const second = await (await fetch(base + '/api/notes')).json();
  console.log(JSON.stringify(second));
  server.close();
  await mongoose.disconnect();
});

Key points

  • The API lists notes, adds notes, and serves them as JSON.
  • React fetches the list and shows each note on the page.
  • A form adds a note, and the list refreshes after the change.
  • Login keeps the notes behind a token check.

In this example, the script starts a fresh database and seeds three notes. It then starts the API and fetches the list. It adds one more note and fetches the list again. The two printed lists show the app growing by one note.

Note

The same three seed notes open the app every time.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does the capstone app do first when it starts?