Giter Club home page Giter Club logo

Comments (14)

devongovett avatar devongovett commented on May 3, 2024 22

I don't really think this should be the responsibility of the bundler... Can you just enable CORS and hit localhost:3001 directly?

Alternatively, you could build your own small server with express and use parcel as a middleware.

let Bundler = require('parcel-bundler');
let express = require('express');

let bundler = new Bundler('entry.html');
let app = express();

// define proxy routes here

app.use(bundler.middleware());

from parcel.

albinotonnina avatar albinotonnina commented on May 3, 2024 12

@yiranlee

const proxy = require('http-proxy-middleware')
const Bundler = require('parcel-bundler')
const express = require('express')

const bundler = new Bundler('src/index.html', {
  cache: false
})

const app = express()

app.use(
  '/api',
  proxy({
    target: 'http://localhost:3000'
  })
)

app.use(bundler.middleware())

app.listen(Number(process.env.PORT || 1234))

from parcel.

samithaf avatar samithaf commented on May 3, 2024 9

Hey @webular , you can probably use browser-sync and http-proxy-middleware to achieve your end goal and it's relatively easy to set it up. E.g.

import assign from 'lodash/assign';
import Bundler from 'parcel-bundler';
import serve from 'browser-sync';
import proxy from 'http-proxy-middleware';
import compress from 'compression';

const bundler = new Bundler('index.html');
serve({
   port: process.env.PORT || 3000,
   open: false,
   server: { baseDir: 'dist', https: true },
   middleware: [
     proxy(`${envConfig.endpoint}/api`, assign({}, proxyConfig)),
     proxy(`${envConfig.endpoint}/auth`, assign({}, proxyConfig)),
     compress(),
     bundler.middleware()
   ],
 }); 

from parcel.

albinotonnina avatar albinotonnina commented on May 3, 2024 6

An complete example with express

const proxy = require('http-proxy-middleware')
const Bundler = require('parcel-bundler')
const express = require('express')

let bundler = new Bundler('src/index.html')
let app = express()

app.use(
  '/api',
  proxy({
    target: 'http://localhost:3000'
  })
)

app.use(bundler.middleware())

app.listen(Number(process.env.PORT || 1234))

from parcel.

ThaddeusJiang avatar ThaddeusJiang commented on May 3, 2024 5

It would be a very useful feature for spa.

from parcel.

tb avatar tb commented on May 3, 2024 4

@devongovett I always use create-react-app proxy This allows me to use relative urls like /api/endpoint and /graphql. The same urls work then after deployment to production server.

from parcel.

kojuka avatar kojuka commented on May 3, 2024

@tb Oh - thats exactly what I want. Thx for the tip!

from parcel.

jylin avatar jylin commented on May 3, 2024

It's not the responsibility of the bundler, but it would be a very useful feature for the dev server. If your server is not in NodeJS (and you don't want to enable CORS), then without this you have to use some kind of post-processing to replace "locaholhost:PORT" with something else when deploying to production. It's much nicer to be able to use relative links and have the dev server have a config to proxy to the appropriate backend endpoint.

from parcel.

chasegiunta avatar chasegiunta commented on May 3, 2024

For the dev-server, I think this would be useful for non-spa sites as well (php applications, etc). Where you proxy the app (myphpsite.test), but watch the assets and/or templates directory for changes. Templates directory changes would trigger full reloads, while asset changes trigger HMR (in a perfect world)

from parcel.

yiranlee avatar yiranlee commented on May 3, 2024

@albinotonnina oh~good example, but how can i use parcel no cache in this example ? like this : node example.js --no-cache or set some parameters in 'app.use(bundler.middleware())'

from parcel.

larrybolt avatar larrybolt commented on May 3, 2024

For those still needing more help, the socketio example chat app altered to bundle the client-side files:
larrybolt/chat-example@aaeba77

from parcel.

albinotonnina avatar albinotonnina commented on May 3, 2024

Hi @NullVoxPopuli,
Don't know really.
To understand: http://${process.env.API_HOST} prints http://localhost:9091 ?

from parcel.

l-hammer avatar l-hammer commented on May 3, 2024

@albinotonnina nice example~ but i want to set --open parameters in new Bundle('src/index.html')? like this:

const bundler = new Bundler(proxypath, {
  open: true,
})

But this has no effect,how to do it? Thx~

from parcel.

JamesTheHacker avatar JamesTheHacker commented on May 3, 2024

For some reason Parcel's dev server automatically responds with the header Access-Control-Allow-Origin : *. Is this normal behaviour? If so surely it solves this problem? I've tested tonight and I get no CORS warnings.

from parcel.

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.