create a http server with a options route in typescript

Here's an example of how to create an HTTP server with an options route using Express and TypeScript:

index.ts
import express from "express";

const app = express();

// Define the OPTIONS route
app.options("/", (req, res) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST");
  res.setHeader("Access-Control-Allow-Headers", "Content-Type");
  res.setHeader("Content-Length", "0");
  res.sendStatus(200);
});

// Start the server
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});
491 chars
19 lines

In this example, we're using the Express framework to create an HTTP server. We define an OPTIONS route at the root path (/) that sets the necessary headers for Cross-Origin Resource Sharing (CORS) and sends a status code of 200.

When the server is started, it listens on port 3000 and logs a message to the console.

Note that in order to run this code, you'll need to have the express package installed (e.g. via npm install express). Also, depending on your use case, you may need to modify the CORS headers and/or allow additional HTTP methods in the Access-Control-Allow-Methods header.

gistlibby LogSnag