Create the Express Application

Open src/server.js and create the initial Express application.

Express will handle regular HTTP requests, such as loading the chat page and checking application health.

We will create an Express application and serve static frontend files from the public directory.

src/server.js
import 'dotenv/config';
import express from 'express';
import { createServer } from 'node:http';

const app = express();

app.use(express.json());
app.use(express.static('public'));

app.get('/health', (req, res) => {
  res.status(200).json({
    status: 'ok',
    message: 'Chat server is running'
  });
});

const httpServer = createServer(app);

const PORT = process.env.PORT || 3000;

httpServer.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}`);
});

Creates the Express application, enables JSON parsing, serves static frontend files, adds a health endpoint, and creates a Node.js HTTP server.

Key Takeaways
  • Express handles HTTP routes and static files.

  • The HTTP server is created using node:http.

  • The health endpoint helps verify application availability.