The purpose of this project is to create an HTTP proxy server to normalize requests with CORS and facilitate API interactions with any request method and on-the-fly modifications.
Project Benefits
This project simplifies the development process by handling CORS issues and providing a flexible way to interact with APIs, making it easier to test and develop applications.
How the Project Works
The project sets up a proxy server using Node.js that intercepts HTTP requests, adds the necessary CORS headers, and allows for modification of the request and response on the fly.
Repository and Installation
To install and run the project:
-
Clone the repository:
Terminal window git clone https://github.com/Fulldroper/cors-proxycd cors-proxy -
Install dependencies and start the server:
Terminal window npm installnpm start
How to use?
Make default requests to /
path and set header cors-url
with site url
For ignore headers set header cors-ignore
with array of headers will be deleted from request
all methods, headers, params duplicated automatically
How change port?
- Create file
.env
- write
PORT=<Your port>
- Save changes
How change enterpoint?
Change or add to file
.env
next lineENTERPOINT=/
How change alowed methods?
Change or add to file
.env
next lineALOWED=GET, POST, PUT, DELETE
Project Workflow
-
Setup Project: Initialize the project structure and dependencies.
Terminal window npm initnpm install -
Create Proxy Server: Set up the proxy server with CORS handling.
const express = require('express');const { createProxyMiddleware } = require('http-proxy-middleware');const app = express();app.use('/', createProxyMiddleware({target: 'http://example.com',changeOrigin: true,onProxyReq: (proxyReq, req, res) => {// Modify request headersproxyReq.setHeader('X-Special-Proxy-Header', 'foobar');},onProxyRes: (proxyRes, req, res) => {// Modify response headersproxyRes.headers['X-Special-Proxy-Response-Header'] = 'baz';}}));app.listen(3000, () => {console.log('CORS proxy server running on port 3000');});
Skills Gained
- Developing proxy servers with Node.js
- Handling CORS issues in web development
- Modifying HTTP requests and responses on the fly