Tuesday, 6 June 2017

How to get my paths correct for static files when using a reverse proxy

I can't figure out why this isn't working. I am passing this a reverse proxy for example mkteagle/admin will send me to mkteagle:8083. But it's like express can't find my static files at mkteagle:8083/dist/script.js. It loads up index.html fine just not the static files in the same folder.

// Get dependencies
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
const cors = require('cors');

// Get our API routes
const api = require('./server/routes/api');

const app = express();
app.use(cors());

// Parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// Point static path to dist
// serve angular front end files from root path
app.use(express.static(path.join(__dirname, 'dist')));
// Set our api routes
app.use('*/api', api);

// Catch all other routes and return the index file
app.get('/*', (req, res) => {
  res.sendFile(path.join(__dirname, 'index.html'));
});


/**
 * Get port from environment and store in Express.
 */
const port = process.env.PORT || '8082';
app.set('port', port);

/**
 * Create HTTP server.
 */
const server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */
server.listen(port, () => console.log(`API running on localhost:${port}`));



via mkteagle

No comments:

Post a Comment