I am running an express.js web app on an Apache2 server. Right now, the app is pretty basic, and all I’m trying to do is print the request URL to the console. For example, when I access https://subdomain.example.com/node/something in my browser, I expect it to print /something
to the console. Instead, it prints //something
(notice the extra slash at the beginning). This means that any route handlers I write would be thrown off. At the moment, I really don’t know what could be causing this.
A point that might be relevant is that I’m using Apache’s mod_rewrite to remove the www. at the beginning of the URL, and the file extensions (.php etc.) from the end. I’ve tried commenting these out to see whether this changes it, but it didn’t.
My app:
const express = require('express');
const PORT = 8080;
const app = express();
app.use((req, res) => {
console.log(req.url);
res.status(200).send()
});
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`)
})
My Apache vhost config:
<IfModule mod_ssl.c>
<VirtualHost *:443>
Alias /fonts /usr/share/fonts/www
<Directory "/fonts">
Require all granted
</Directory>
<Directory "/errors">
Require all denied
</Directory>
<Directory "/">
DirectoryIndex login.php
</Directory>
ServerName admin.example.com
ServerAlias www.admin.example.com
DocumentRoot /var/www/admin.example.com
ErrorDocument 403 /errors/nope.html
ErrorDocument 404 /errors/nope.html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
LogLevel alert rewrite:trace6
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
<Location /node>
ProxyPass http://127.0.0.1:8080/
ProxyPassReverse http://127.0.0.1:8080/
</Location>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.admin.example.com$
RewriteRule ^/(.*)$ http://admin.example.com/$1 [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=301]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^(.+).php$
RewriteRule (.*).php$ $1 [R=301,L]
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/admin.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/admin.example.com/privkey.pem
</VirtualHost>
</IfModule>
Any help would be much appreciated.
Update: I moved the .js file out of the /node folder and into the root directory of the Apache site, and that fixed the issue somehow. However, I still want to be able to move it into a folder without having problems.
2
Answers
My folder structure was like this:
Project
---- index.php
---- node
-------- main.js
Fixed by changing this in the vhost config:
Not great with apache but what happens if you remove the / from the end of
RewriteRule ^/(.*)$ http://admin.example.com/$1 [R=301,L]
and instead have it read
RewriteRule ^/(.*)$ http://admin.example.com$1 [R=301,L]
Also Express likes to add / when they don’t need to so I’ve gotten around that by removing them where they aren’t really needed.
take this with a grain of salt though. Like i said Im not very used to Apache and I’ve only been working with express for about a year so both of these could be completely wrong.