Skip to content

Instantly share code, notes, and snippets.

@devansvd
Created December 12, 2018 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devansvd/ba4299d5006404951ebed0def226e559 to your computer and use it in GitHub Desktop.
Save devansvd/ba4299d5006404951ebed0def226e559 to your computer and use it in GitHub Desktop.
//Angular 2 above application static server
const http = require('http');
const express = require('express');
const proxy = require('http-proxy-middleware');
const path = require('path');
const app = express();
app.use(express.static('client'));
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
})
app.set('port', (process.env.PORT || 3004));
// Add middleware for http proxying
const apiProxy = proxy('/api', {
target: 'http://192.168.2.11:8000'
});
app.use('/api', apiProxy);
// Render your site
const renderIndex = (req, res) => {
res.sendFile(path.resolve(__dirname, 'client/index.html'));
}
app.get('/*', renderIndex);
http.createServer(app).listen(app.get('port'), function() {
console.log(`server is running ... http://localhost:${app.get('port')}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment