Intermediate 25 min

Start from Something Familiar

We’ll build the API locally first. This is easier to debug and test. Once it works, we’ll adapt it for AWS.

Step 1: Create Project Folder

Open your terminal and run:

mkdir cloud-notes-api
cd cloud-notes-api

This creates a new folder and moves you into it.

Step 2: Initialize Node Project

Run:

npm init -y

This creates a package.json file. The -y flag says “yes to everything” so you don’t have to answer questions.

You should see:

Wrote to /path/to/cloud-notes-api/package.json

Step 3: Install Express

Express is a web framework for Node.js. It makes building APIs easy.

npm install express

This downloads Express and adds it to your package.json.

Step 4: Create the API Code

Create a file called index.js:

touch index.js

Open index.js in your editor and add this code:

const express = require('express');
const app = express();

// Middleware to parse JSON bodies
app.use(express.json());

// In-memory storage (we'll lose this when server restarts)
let notes = [
  { id: 1, text: 'Buy milk' },
  { id: 2, text: 'Call mom' }
];
let nextId = 3;

// Health check endpoint
app.get('/health', (req, res) => {
  res.json({
    status: 'ok',
    timestamp: new Date().toISOString()
  });
});

// Get all notes
app.get('/notes', (req, res) => {
  res.json({ notes });
});

// Add a new note (optional bonus)
app.post('/notes', (req, res) => {
  const { text } = req.body;
  
  if (!text) {
    return res.status(400).json({ error: 'text is required' });
  }
  
  const newNote = {
    id: nextId++,
    text: text
  };
  
  notes.push(newNote);
  res.status(201).json(newNote);
});

// Start server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});

Understanding the Code

Let’s break it down:

Line 1-2: Import Express and create an app instance.

Line 5: app.use(express.json()) - This middleware parses JSON request bodies. Without it, req.body would be undefined.

Line 8-11: In-memory storage. Notes are stored in an array. When the server restarts, they’re gone. That’s fine for now.

Line 14-19: Health check route. Returns status and timestamp. Useful for monitoring.

Line 22-25: GET /notes - Returns all notes as JSON.

Line 28-42: POST /notes - Adds a new note. Validates that text exists, creates a new note with an ID, adds it to the array, and returns it.

Line 45-48: Start the server. Uses process.env.PORT if set, otherwise defaults to 3000.

Step 5: Test Locally

Start the server:

node index.js

You should see:

Server running on http://localhost:3000

Now test it. Open a new terminal window and run:

Test health endpoint:

curl http://localhost:3000/health

You should see:

{"status":"ok","timestamp":"2025-12-02T10:00:00.000Z"}

Test GET notes:

curl http://localhost:3000/notes

You should see:

{"notes":[{"id":1,"text":"Buy milk"},{"id":2,"text":"Call mom"}]}

Test POST notes:

curl -X POST http://localhost:3000/notes \
  -H "Content-Type: application/json" \
  -d '{"text":"New note"}'

You should see:

{"id":3,"text":"New note"}

Verify it was added:

curl http://localhost:3000/notes

You should now see three notes.

Alternative: Use Your Browser

You can also test in your browser:

  1. Open http://localhost:3000/health
  2. Open http://localhost:3000/notes

For POST requests, use a tool like Postman or curl.

Concepts: Port Binding

What is a port? A port is like a door number. Your computer has many ports (0-65535). Port 3000 is where your server listens.

Why 3000? It’s a common default. You can use any port (3000, 8000, 5000, etc.). Just make sure nothing else is using it.

Environment variables: process.env.PORT lets you change the port without editing code. Useful for deployment.

PORT=8000 node index.js  # Runs on port 8000

Concepts: Environment Variables

Environment variables are settings that come from outside your code.

Why use them?

  • Different settings for development vs production
  • Keep secrets out of code
  • Easy to change without editing code

Common ones:

  • PORT - Which port to use
  • NODE_ENV - development or production
  • DATABASE_URL - Database connection string (we’ll use this later)

Set them:

PORT=3000 NODE_ENV=development node index.js

Or create a .env file (we won’t do this now, but you can later).

Troubleshooting

“Port already in use” error:

  • Another program is using port 3000
  • Solution: Change the port or stop the other program

“Cannot find module ‘express’” error:

  • You forgot to run npm install
  • Solution: Run npm install in the project folder

“Cannot GET /” error:

  • You’re trying to access the root path /
  • Solution: Use /health or /notes instead

Key Takeaways

Before moving to the next page:

  1. Express makes APIs easy - Simple routes, JSON parsing built-in
  2. Test locally first - Easier to debug than in the cloud
  3. Port binding - Server listens on a specific port
  4. Environment variables - Settings from outside the code
  5. In-memory storage - Simple but temporary (lost on restart)

What’s Next?

In the next page, we’ll adapt this code to work with AWS Lambda. The main change: instead of app.listen(), we’ll export a handler function.