Coder Social home page Coder Social logo

Comments (18)

vssathish avatar vssathish commented on May 5, 2024 4

I am doing proxy using proxy-middleware. Modified srcServer.js

// This file configures the development web server
// which supports hot reloading and synchronized testing.

// Require Browsersync along with webpack and middleware for it
import browserSync from 'browser-sync';
import webpack from 'webpack';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import url from 'url';
import proxy from 'proxy-middleware';

import webpackConfigBuilder from '../webpack.config';

const webpackConfig = webpackConfigBuilder('development');
const bundler = webpack(webpackConfig);

var proxyOptions = url.parse('http://myproxyserver.net);
proxyOptions.route = '/api';

// Run Browsersync and use middleware for Hot Module Replacement
browserSync({
server: {
baseDir: 'src',
middleware: [
webpackDevMiddleware(bundler, {
// Dev middleware can't access config, so we provide publicPath
publicPath: webpackConfig.output.publicPath,
// pretty colored output
stats: { colors: true },
// Set to false to display a list of each file that is being bundled.
noInfo: true
// for other settings see
// http://webpack.github.io/docs/webpack-dev-middleware.html
}),
// bundler should be the same as above
webpackHotMiddleware(bundler),
proxy(proxyOptions)
]
},
// no need to watch '.js' here, webpack will take care of it for us,
// including full page reloads if HMR won't work
files: [
'src/
.html'
]
});

from react-slingshot.

coryhouse avatar coryhouse commented on May 5, 2024 3

I'm reopening since I'd love to see a PR on this. :)

from react-slingshot.

kwelch avatar kwelch commented on May 5, 2024 1

Closing. Discussion further in the PR #93

from react-slingshot.

coryhouse avatar coryhouse commented on May 5, 2024 1

@kwelch You're a man on a mission tonight! Love it! 💯

from react-slingshot.

kwelch avatar kwelch commented on May 5, 2024 1

I saw this tweet from @mjackson and got very inspired.

from react-slingshot.

coryhouse avatar coryhouse commented on May 5, 2024

Great question. Can you point me to a working configuration example?

from react-slingshot.

prpatel avatar prpatel commented on May 5, 2024

@coryhouse I assume you're asking for an example in webpack-dev-server. Have a look at this file in one of my repos:
https://github.com/prpatel/react-workshop-day1/blob/master/Lab12/webpack.config.js

from react-slingshot.

coryhouse avatar coryhouse commented on May 5, 2024

So to be clear, this is an alternative to enabling CORS on local APIs, right? I suspect it's possible to pull this off along with browser-sync, but I haven't had time to hunt for an example yet.

from react-slingshot.

prpatel avatar prpatel commented on May 5, 2024

Actually, it is more about a seamless development experience / setup than CORS. Allow me to elaborate. I'm running my Rails/Node/Java/Python/etc backend server and want to create a SPA using React. During development, I need to either do one of these things to make this work:

  1. use ENV vars to set the base URL to my backend API server
  2. use magic vars (let authUrl = "http://localhost:3000";) that are managed using (1) or some other method
  3. Proxy API calls with some simple configuration through browsersync+webpack in this project's setup

IMO, (3) is not the easest, but is the most simple. From the research I've done into this, I think the answer may be to invert the browsersync+webpack setup:
https://www.npmjs.com/package/browser-sync-webpack-plugin
but I'm not a browersync expert, and haven't had time to dig into its guts to determine how this is possible - was hoping that someone here had already tackled this and had a quick answer.

from react-slingshot.

nohmar avatar nohmar commented on May 5, 2024

@prpatel did you find a solution for this? Mind sharing it here?

from react-slingshot.

prpatel avatar prpatel commented on May 5, 2024

The solution posted by @vssathish above works well.

from react-slingshot.

vssathish avatar vssathish commented on May 5, 2024

I am unable to push my branch. Do i need special permissions?

from react-slingshot.

coryhouse avatar coryhouse commented on May 5, 2024

@vssathish You don't need any special permissions to submit a pull request.

from react-slingshot.

vssathish avatar vssathish commented on May 5, 2024

Submitted a PR.

#93

from react-slingshot.

 avatar commented on May 5, 2024

@vssathish in this sample does your '../webpack.config' look identical to the OP? I do not see a 'webpack.config' in the root directory of the project only the webpack.config.dev and webpack.config.prod

from react-slingshot.

vssathish avatar vssathish commented on May 5, 2024

@frankbaum the changes are only to srcServer.js

from react-slingshot.

 avatar commented on May 5, 2024

@vssathish I see your fork of the project - perhaps there was a change and new config files were added after you forked it - as there is not a webpack.config in the existing source there is a .dev and .prod version - thanks

from react-slingshot.

nickytonline avatar nickytonline commented on May 5, 2024

@vssathish, I'm a new collaborator on the project and am just going through the issues. Just wondering if you have any blockers to submit your PR?

from react-slingshot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.