Express proxy in node

Hello everyone and happy holidays.

Was wondering if you could maybe give me some hints on how to route traffic to different ports in glitch.
I’m pretty new to this, tried googling my way to the goal… But here i am :smiley:

I was hoping to achieve this result:
Copied from user etamponi from thread What's the port range users have at Glitch?
(Sadly im still missing something even after reading the post)

https://your-project.glitch.me/serviceA/:path -> http://localhost:4000/:path
https://your-project.glitch.me/serviceB/:path -> http://localhost:5000/:path
https://your-project.glitch.me/serviceC/:path -> http://localhost:6000/:path

I have 3 node files running at the same time on different ports.
I did a curl http://localhost:3002/login and curl http://localhost:3001/MessagingClient from the terminal in glitch and got something back.
But when i try to browse my app https://messaginmountaineer.glitch.me/login am getting Cannot GET /login
What am I missing?

App.js

const dotenv = require("dotenv").config();
const express = require("express");
const proxy = require('express-http-proxy');
const morgan = require("morgan")
require("./testLoginExpress");
require("./MessagingServer");

const app = require("express")();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const port = process.env.PORT || 3000;
app.use(morgan("dev"))

app.use('/login', proxy("http://localhost:3002/Login"))
app.use('/chat', proxy("http://localhost:3001/MessagingClient"))

http.listen(port, () => {
  console.log(`App login server running at http://localhost:${port}/`);
});

testLoginExpress.js

const dotenv = require("dotenv").config();
const proxy = require('express-http-proxy');
const express = require("express");
const morgan = require("morgan");
const app = require("express")();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const port = process.env.PORT2|| 3002

const { readFromDB } = require("./mongoDB");
app.use(morgan("dev"));
app.use(express.static(__dirname +"public"));

app.get("/Login", (req, res) => {
   res.sendFile(__dirname + "/public2/LoginClient.html");

});

http.listen(port, () => {
  console.log(`Express login server running at http://localhost:${port}/`);
});

MessagingServer.js

const dotenv = require("dotenv").config();
const express = require("express");
const proxy = require('express-http-proxy');
const app = require("express")();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const port = process.env.PORT || 3001;
require("./testLoginExpress");
const { readFromDB } = require("./mongoDB");

app.use(express.static(__dirname + "/public/"));
app.get("/MessagingClient", (req, res) => {
  res.sendFile(__dirname + "/public/MessaginClient.html");
});

//some logic

http.listen(port, () => {
  console.log(`Socket.IO server running at http://localhost:${port}/`);
});

1 Like

Hi! I’ve been testing that and error means it can’t find the localhost.

Thanks for testing, i guess am going back to the drawing board and google some more.

can you check if that proxy package express-http-proxy really supports an argument like "http://localhost:3002/Login"? I’m looking in its readme and it’s mostly showing examples like this, with no http:// and no path:

app.use(proxy('localhost:12345', { ... }));

Hey, thanks for checking it out.
I ended up running all my files on a local linux server with nginx to route the traffic.

Not sure where I’ll end up in production, but will give it a try when that time comes.
Thanks again :slight_smile:

Why not just use nginx?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.