Giter Site home page Giter Site logo

Usage with http-server about choo HOT 1 OPEN

rook2pawn avatar rook2pawn commented on May 27, 2024
Usage with http-server

from choo.

Comments (1)

rook2pawn avatar rook2pawn commented on May 27, 2024

Ah, https://stackoverflow.com/questions/39744309/node-http-server-to-respond-with-index-html-to-any-request

has a good discussion on this topic and the question mark is really important here and will solve it.

http-server --port 8080 -P http://localhost:8080?

Do you think its worth having a section devoted to running a SPA server with choo?

I also want to point out that I use something like in my package.json

"start": "http-server ./web -o -c-1 -p 8080 --proxy http://localhost:8080?"

And that if you do something like this, make sure you point your src or other local lookups to / first otherwise the proxy would load src="bundle.js" to the proxied path.

   <script src="/bundle.js"></script>

Also, it is common to have a BE resource that you want proxied and I want to share how I did this for comment or to help others.

const http = require("http"),
  httpProxy = require("http-proxy");

const proxy = httpProxy.createProxyServer();
const server = http.createServer(function (req, res) {
  if (req.url.indexOf("/api/") > -1)  {
    proxy.web(req, res, { target: "http://127.0.0.1:5150" });
  } else {
    proxy.web(req, res, { target: "http://localhost:8080" });
  }
});

server.listen(5050);

Now your proxy sits behind a router where all requests go into your http/s server on 5050 then requests either go to http-server or they are marked as back-end with /api/. When requests go to http-server, the catch-all redirect works for your SPA. The only thing you have to manage is the cookie path in your set headers if you use cookies in your Backend node server and you rewrite the url after the if statement to check for /api/

Now you have both SPA with catchall redirect and BE proxying logic with http-proxy. The author of http-server don't really have a clear focus on serving SPA needs so I think this method might be fill in some needed gaps.

from choo.

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.