I made a node js express code that allow me to connect to a server ( reverse proxy ) from apache , so I made this script to when I go to the url it logged in automatically , but it display this error :
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ClientRequest.setHeader (node:_http_outgoing:703:11)
at ProxyServer. (file:///Users/mohamedyassinemessaoud/Documents/Yassine/PFE/Backend/server.js:87:12)
my code is like that
Code Nodejs
import httpProxy from 'http-proxy';
let serverProxy = httpProxy.createProxyServer();
const username = 'anthony';
const password = 'anthony';
const basicAuth = 'Basic ' + Buffer.from(`${username}:${password}`).toString('base64');
serverProxy.on('proxyReq', function(proxyReq, req, res) {
// Set headers before sending the response
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.setHeader('Access-Control-Allow-Credentials', true);
// Set Authorization header for Basic Auth if body is present
if (!proxyReq.getHeader('Content-Length') && req.body) {
const bodyData = JSON.stringify(req.body);
proxyReq.setHeader('Content-Type', 'application/json');
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
}
// Set Basic Auth header
proxyReq.setHeader('Authorization', basicAuth);
});
app.all('/dashboard(/*)?', (req, res) => {
console.log("Rewritten URL:", req.url);
serverProxy.web(req, res, {
target: 'http://localhost:80',
prependPath: false
});
});
and my httpd.conf from apache server ::
ServerRoot "/usr/local/apache2"
Listen 80
# Load MPM module - choose one based on your needs
LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule env_module modules/mod_env.so
LoadModule headers_module modules/mod_headers.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
<IfModule unixd_module>
User daemon
Group daemon
</IfModule>
ServerAdmin localhost
<Directory />
AllowOverride none
Require all denied
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header set Access-Control-Allow-Headers "Content-Type, Authorization"
</Directory>
DocumentRoot "/usr/local/apache2/htdocs"
ErrorLog /proc/self/fd/2
LogLevel error
<IfModule log_config_module>
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %I %O" combinedio
</IfModule>
CustomLog /proc/self/fd/1 common
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
Header set Access-Control-Allow-Headers "Authorization, Content-Type"
</IfModule>
<Proxy *>
AuthType Basic
AuthName "GrafanaAuthProxy"
AuthBasicProvider file
AuthUserFile "/tmp/htpasswd"
Require valid-user
RewriteEngine On
RewriteRule .* - [E=PROXY_USER:%{LA-U:REMOTE_USER},NS]
RequestHeader set X-WEBAUTH-USER "%{PROXY_USER}e"
</Proxy>
RequestHeader unset Authorization
ProxyRequests Off
ProxyPass / http://grafana:3000/
ProxyPassReverse / http://grafana:3000/
how to fix it emergency please, and thanks