Express Routes serving up as expected on local machine. But not when on Nginx

265
January 06, 2018, at 07:24 AM

Routes serving up as expected on my local machine. But not serving up /public or /api routes.

I've setup a serverblock for node.mydomain.com, installed a node app, and am getting 404 errors for all the routes express is supposed to be serving (static and api).

node.mydomain.com => /public/index.html (works fine)

node.mydomain.com/style.css => /public/style.css (404 error)

node.mydomain.com/api/puppies => /api (404 error)

This app should run in the root of my subdomain (node.mydomain.com) which is actually localhost:3000. What am I doing wrong?

NGINX SERVER BLOCKS

#server {
listen 80;
listen [::]:80;
root /var/www/node.mydomain.com/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name node.mydomain.com www.node.mydomain.com;
location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # node app hello.js
            proxy_pass http://localhost:3000;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_buffering off;
    }
    location /app2 {
            proxy_pass http://localhost:3000;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_buffering off;
    }
}

EXPRESS ROUTES

const express = require('express');
const app = express();
const path = require('path');
app.use(express.static(path.join(__dirname, '../public')));
app.use('/bootstrap', express.static(path.join(__dirname, '..', '/node_modules/bootstrap/dist')));
app.use('/api', require('./api'));

// handle 404s
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, '../public/index.html'));
});
// handle 500s
app.use(function (err, req, res, next) {
console.error(err);
console.error(err.stack);
res.status(err.status || 500).send(err.message || 'Internal server error.');
});
const port = process.env.PORT || 3000;
app.listen(port, function () {
    console.log(`Your server, listening on port ${port}`);
    console.log(`Browse to http://localhost:${port} to view your app`);
});

API ROUTES

//api root
router.get('/', function (req, res, next) {
res.send('api root');
});
// /api/campuses
router.get('/campuses', function (req, res, next) {
Campuses.findAll({})
.then(campuses => res.json(campuses))
.catch(next);
});
Rent Charter Buses Company
READ ALSO
monitor Windows pc on lan using … what technologies and tools to use? laravel,node.js?

monitor Windows pc on lan using … what technologies and tools to use? laravel,node.js?

i want to develop a simple server which give me a web page dashboard for monitor Windows pc on lan and their informations (such connected user)I'm thinking of a server (i

166
node js mongoose mongoDB

node js mongoose mongoDB

Using phpstorm, trying to get mongoDB server up and running using nodeJS, i have mongo installed and have created the required folders on the c drive to store database, proceeded by going to command prompt and entering mongod, to get mongo db running

299
“direct_message” event not working JavaScript (node js)

“direct_message” event not working JavaScript (node js)

I am working on twit APIThe event "direct_message" will be triggered when someone send me message

209
Permission denied & files/programs not found - Ubuntu on Windows 10 using Hyper

Permission denied & files/programs not found - Ubuntu on Windows 10 using Hyper

I have used Ubuntu on Windows 10 in the past and it worked perfectly fine(back then i just enabled the subsystem and used ubuntu in store

320