My goal is to build a contact form for a static website (pure html/css/js). It's on a server using Nginx which serves a single index.html file. index.html includes a Javascript file that is called when the contact form is submitted. This JS file awaits axios to make a POST request to localhost:3000/contact, which is a NodeJS app that I wrote running on that port, and sends the e-mail using nodemailer.
When I submit the contact form on my local computer, everything works fine (I receive an email). But when I try it on my server using Nginx, it fails. I receive the error: 405 not allowed. It also looks like the server function is never called.
My first question is: is this even possible using Nginx?
If so, then what does my config file need to look like? Here is the base file:
server {
listen 80 default_server;
server_name www.website.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name www.website.com;
ssl_certificate /etc/nginx/ssl/website-com-cert.pem;
ssl_certificate_key /etc/nginx/ssl/website-com-key.pem;
location / {
root /var/www/html;
index index.html index.htm;
}
}
I've tried every suggestion from this question and none of them have worked. Setting 405 = 200 removes the error message but the NodeJS server never gets the function call to send the email (if it did, that would work for me). Or the other suggestions will show an error "Cannot POST /" or "Cannot POST /index.html" on a blank screen when the contact form submits. It just gets worse from there.
Most questions involving NodeJS servers on Nginx assume that the whole thing is a singular app, so they make it a reverse proxy to localhost:port or something. But I want to serve index.html as a static file, which then makes a POST request to an app running on a port on the same server.
I can't believe that a contact form for a static site is this difficult to code. What could I be doing wrong? Is this even possible? Am I going about this completely the wrong way? I don't really NEED it to use Nginx specifically, but how else will I serve the static file? And I don't want to use a third-party forms SaaS that "does the work for me" -- it needs to be done myself.