javascript .get function works in development but not production

Viewed 241

Below is code where the client is trying to get data from the server. Locally running my server on node.js, this works great. However, when I push to my server, I get /getSettings 500 (Internal Server Error). My index gets hit when the other request is made '/' so I know the endpoint can be hit. But why not this other get?

Node.js is running on my server as a runtime.

SERVER code (app.js):

const express = require('express');
var path = require('path');
var Config = require('./config.js'), conf = new Config();
const app= express();
const port = 3000;

app.listen(port, function(error){
if(error) {
    console.log('Server failed to listen: ', error)
} else{
    console.log('Server is listening on port: ' + port)
}
});

app.get('/', function (req, res) {
   res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

app.get("/getSettings", function(require, response){
  response.json({ configuration: conf });
});

app.use(express.static(path.join(__dirname, 'public')));

CLIENT Code:

 <script>
    window.onload = function() 
    {   
        $.get("/getSettings", function( data ) 
        {
            //do stuffs
        });
    };
</script>
1 Answers

I found a fix. /getSettings needs an empty folder called getSettings. So if anyone else has a scenario where root works fine, but no other routes do, this may likely be your issue.

Related