UNPKG

1.96 kBJavaScriptView Raw
1var express = require('express')
2var webpack = require('webpack')
3var config = require('./webpack.example.conf')
4var proxyMiddleware = require('http-proxy-middleware')
5var httpProxy = require('http-proxy')
6
7var app = express()
8var compiler = webpack(config)
9var proxy = httpProxy.createProxyServer()
10
11// Define HTTP proxies to your custom API backend
12// https://github.com/chimurai/http-proxy-middleware
13var proxyTable = {
14 // '/api': {
15 // target: 'http://jsonplaceholder.typicode.com',
16 // changeOrigin: true,
17 // pathRewrite: {
18 // '^/api': ''
19 // }
20 // }
21}
22
23var devMiddleware = require('webpack-dev-middleware')(compiler, {
24 publicPath: config.output.publicPath,
25 stats: {
26 colors: true,
27 chunks: false
28 }
29})
30
31var hotMiddleware = require('webpack-hot-middleware')(compiler)
32// force page reload when html-webpack-plugin template changes
33compiler.plugin('compilation', function (compilation) {
34 compilation.plugin('html-webpack-plugin-after-emit', function (data, cb) {
35 hotMiddleware.publish({ action: 'reload' })
36 cb()
37 })
38})
39
40// proxy api requests
41Object.keys(proxyTable).forEach(function (context) {
42 var options = proxyTable[context]
43 if (typeof options === 'string') {
44 options = { target: options }
45 }
46 app.use(proxyMiddleware(context, options))
47})
48
49// handle fallback for HTML5 history API
50app.use(require('connect-history-api-fallback')())
51
52// serve webpack bundle output
53app.use(devMiddleware)
54
55// enable hot-reload and state-preserving
56// compilation error display
57app.use(hotMiddleware)
58
59// serve pure static assets
60app.use('/static', express.static('./static'))
61
62app.all('/rs/*', function (req, res) {
63 proxy.web(req, res, {
64 target: 'http://127.0.0.1:8081/ldap'
65 })
66})
67
68module.exports = app.listen(8080, function (err) {
69 if (err) {
70 console.log(err)
71 return
72 }
73 console.log('Listening at http://localhost:8080')
74})