Here's an example of how to create a simple REST API using the Node.js built-in http module:
index.tsx580 chars20 linesThis creates an HTTP server that listens for requests on port 3000. When a GET request is made to /api/data, the server responds with some JSON data ({ message: 'Hello, world!' }). For all other requests, the server returns a 404 error.
However, using a framework like Express.js can simplify the process of building a REST API. Here's an example using Express:
index.tsx331 chars15 linesThis code does the same thing as the previous example, but with less boilerplate. Express provides methods for defining routes and handling requests, making it easier to build a REST API.
gistlibby LogSnag