NodeJS Server on Raspberry Pi not calling function

by Ashley   Last Updated February 17, 2018 20:01 PM

I currently have a raspberry pi running Raspian OS which a Node JS server running on port 8080. I then setup NGINX to be used as a reverse proxy to route incoming port 80 traffic to my server at 8080.

I am trying to have the user click an HTML button which then calls a NodeJS function to redirect them to google. Now I don't think the problem lies in the code because if I run the server on my local computer (no nginx required) everything works fine. However when I try to access the server running on the Raspberry Pi from my local computer: http://192.168.0.103/Authenticate.html works and serves the HTML page with the button but then the function call http://192.168.0.103/Authenticate.js gives me a 404 error.

I believe I have something setup wrong in this reverse proxy, but I am VERY new to this so I have no idea what. Any help is appreciated.

NGINX default file

    {
    listen 80;
    listen [::]:80;
    root /home/pi/Cortana;
    server_name _;
    location / {
        proxy_pass http://127.0.0.1:8080;
        try_files $uri 4uri/ =404;
    }
}

OAuthTest.html

<form method="post" action="/Authenticate.js">
    <input type="submit" value="Login with Reddit" >
</form>

OAuthTest.js

const http = require('http');
const request = require('request');
const fs = require('fs');
const express = require('express');

const app = express();
const server = http.createServer(app);

app.get('/', serveDefaultHTML);
app.get('/OAuthTest.html', serveAuthHTML);
app.post("/Authenticate.js", function(req, res){
    console.log("Here");
    redirecting(req, res);
    });


server.listen(8080, function(){
    console.log("Listening on http://localhost:8080");
});

function serveAuthHTML(req, res){
    fs.readFile("OAuthTest.html", function(err, data){
        if (err){
            res.writeHead(404, {'Content-Type': 'text/html'});
            res.end(err);
            return;
        }
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end(data);
    });
}

function serveDefaultHTML(req, res){
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write("Welcome to my server!")
    res.end("Request url: " + req.url);
}

function redirecting(req, res){
    console.log("Redirecting");
    res.writeHead(301,
        {Location: 'http://www.google.com'}
    );
    res.end();
}


Related Questions




Nginx reverse proxy to different server internally

Updated January 28, 2018 21:01 PM

Can't get nginx as a proxy server working

Updated May 18, 2015 05:00 AM