To add CORS headers to a HTTP server with a specific origin in TypeScript, we can use the cors
middleware package.
First, install the package using npm
:
17 chars2 lines
Then, create a new server using the http
module and define the CORS options object with the desired origin:
index.ts206 chars12 lines
Next, install the cors
middleware to your server and start listening:
index.ts185 chars8 lines
With this code, your server will now only accept requests from the specified origin.
gistlibby LogSnag