Created
December 12, 2018 12:39
-
-
Save devansvd/ba4299d5006404951ebed0def226e559 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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