Servers
server.jsread-only
const express = require("express");
const app = express();
app.use(express.json());

const users = [
	{
		id: 1,
		name: "Alice Johnson",
		email: "alice@example.com",
		role: "admin"
	},
	{
		id: 2,
		name: "Bob Smith",
		email: "bob@example.com",
		role: "user"
	},
	{
		id: 3,
		name: "Charlie Brown",
		email: "charlie@example.com",
		role: "user"
	}
];

app.get("/", (req, res) => {
	res.json({
		name: "Express API",
		version: "1.0.0",
		endpoints: [
			"/api/users",
			"/api/time",
			"/api/random"
		]
	});
});

app.get("/api/users", (req, res) => {
	console.log("GET /api/users");
	res.json(users);
});

app.get("/api/users/:id", (req, res) => {
	console.log("GET /api/users/" + req.params.id);
	const user = users.find((u) => u.id === Number(req.params.id));
	if (user) res.json(user);
	else res.status(404).json({ error: "User not found" });
});

app.get("/api/time", (req, res) => {
	console.log("GET /api/time");
	res.json({
		timestamp: Date.now(),
		iso: new Date().toISOString(),
		readable: new Date().toLocaleString()
	});
});

app.get("/api/random", (req, res) => {
	console.log("GET /api/random");
	res.json({
		number: Math.random(),
		dice: Math.floor(Math.random() * 6) + 1,
		uuid: "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
			const r = Math.random() * 16 | 0;
			return (c === "x" ? r : r & 3 | 8).toString(16);
		})
	});
});
const server = app.listen(3e3, () => {
	console.log("Express server running on port 3000");
});
globalThis.__server = server;
module.exports = app;

An Express.js REST API server running client-side. It boots, installs dependencies, and runs a full Express application with multiple endpoints, middleware, and an interactive API explorer for testing routes.

How it works

Try it yourself

  1. Click on any route in the API explorer to send a request
  2. Watch the terminal for server logs and responses
  3. Observe the server status indicator in the header