Ask ChatGPT: Node.js Express Math API

This article is part of a series of ChatGPT experiments, in which I give ChatGPT a prompt, share its response, and offer some reflections.

Prompt

Write a Node.js application that uses Express to provide a REST API. There should be an API endpoint for each basic math operation: addition, subtraction, multiplication, division, and modulus. Each endpoint should receive the input numbers as query parameters and return a single number as a response.

Response

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

app.get("/add", (req, res) => {
  const a = Number(req.query.a);
  const b = Number(req.query.b);
  const result = a + b;
  res.send({ result });
});

app.get("/subtract", (req, res) => {
  const a = Number(req.query.a);
  const b = Number(req.query.b);
  const result = a - b;
  res.send({ result });
});

app.get("/multiply", (req, res) => {
  const a = Number(req.query.a);
  const b = Number(req.query.b);
  const result = a * b;
  res.send({ result });
});

app.get("/divide", (req, res) => {
  const a = Number(req.query.a);
  const b = Number(req.query.b);
  const result = a / b;
  res.send({ result });
});

app.get("/modulus", (req, res) => {
  const a = Number(req.query.a);
  const b = Number(req.query.b);
  const result = a % b;
  res.send({ result });
});

app.listen(3000, () => {
  console.log("Math API running on port 3000");
});

This application creates an instance of the Express application, and sets up endpoints for each of the five basic math operations. The endpoints receive the input numbers as query parameters, perform the specified operation, and return the result as a single number in a JSON response. The application listens on port 3000 and logs a message to the console when it is ready to receive requests.

Reflections

This was impressive, and while the code may look a little repetitive, it is very clean and has no unnecessary abstractions.

I did have to submit this same prompt to ChatGPT twice because it stopped responding after spitting out 4 lines of code the first time. However, it finished writing the program smoothly the second time.

I ran the code and tested all of the endpoints just to enjoy seeing them work. The code is so clean and simple that I didn't expect any problems. All five math operations work smoothly.

One thing I really appreciated was the explanation of how this solution works. ChatGPT gave me an overview of the program in plain English that played on my initial prompt while offering specific details about the implementation.

The only "error" with this output is that the code section was labeled as SCSS in the ChatGPT interface. However, the actual syntax highlighting seemed to be JavaScript. I can't say how much of the code labeling and highlighting is controlled by ChatGPT itself and how much of that functionality belongs to the UI in front of ChatGPT, so I can't count this as a miss on ChatGPT's part.

Overall, this was a very promising experiment, and I look forward to using ChatGPT to quickly prototype and scaffold APIs in the future.