Created
January 10, 2017 06:58
-
-
Save lamogura/9ad1f9ab7ab7e3e72bfb55764ae654fa to your computer and use it in GitHub Desktop.
brunch proxied dev server
This file contains hidden or 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
| // 1. npm i -D express http-proxy | |
| // 2. add to config: "server: { path: 'dev_server' }" | |
| const express = require('express') | |
| const sysPath = require('path') | |
| const http = require('http') | |
| const httpProxy = require('http-proxy') | |
| const apiProxy = httpProxy.createServer({ | |
| target: 'http://localhost:3000' | |
| }) | |
| // https://github.com/brunch/brunch-guide/blob/master/content/en/chapter10-web-server.md | |
| exports.startServer = function (port, path, done) { | |
| const app = express() | |
| app.use(express['static'](path)) | |
| app.all('/api/*', (req, res) => { | |
| return apiProxy.web(req, res) | |
| }) | |
| app.all('/*', (req, res) => { | |
| return res.sendFile(sysPath.resolve(sysPath.join(path, 'index.html'))) | |
| }) | |
| server = http.createServer(app) | |
| server.listen(parseInt(port, 10), done) | |
| return server | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment