Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
How can I redirect the user to a custom URL scheme with Express.js and node-fetch?

Sorry if my usage of server-related words is wrong, I'm new to this. I have two Express.js servers one on port 3000 and one on port 8000. The browser renders two different HTML files on these two ports. First I start the server on port 8000. As soon as I start the server on port 3000, I want to redirect the user viewing the site on port 8000 to a custom URL scheme to open an installed app (using "example://"). At the moment I console.log "received" on port 8000 as soon as the other server starts. How can I redirect the user to the URL "example://" so that the app opens?

This is my code for server one (port 3000):

import express, { response } from "express";
import fetch from "node-fetch";
import * as path from 'path';
import { fileURLToPath } from "url";


const touchpointApp = express();
const port = 3000;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

touchpointApp.get('/', (req, res) => {
    res.sendFile('/index.html', { root: __dirname });
});

touchpointApp.listen(port, () => {
    console.log('Running on Port 3000');
    fetch("http://192.168.2.127:8000/launch").then(res => {console.log("Success")});
    
})

And this is my code for server two (port 8000):

const { response } = require('express');
const express = require('express');
const open = require('open');
const router = express.Router();
const path = require('path');
const smartMirror = express();


router.get('/', function(req, res){
    res.sendFile(path.join(__dirname + '/index.html'));
});

smartMirror.use('/', router);
smartMirror.listen(process.env.port || 8000);

console.log('Running on Port 8000');


smartMirror.get("/launch", (req, res) => {
    console.log("Received");
    res.status(200);
})

The code is currently Frankenstein's monster because of the previous tests. I'm using NodeJS to start the servers.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

This is my understanding of your intent:

  1. User in browser visits http://somehost:8000/someUrl and gets a page
  2. You start server on port 3000, the 8000 server somehow detects this
  3. Subsequent requests to http://somehost:8000/someUrl are now redirected to http://somehost:3000/differentUrl and hence the user is now navigating among pages in the 3000 server. Note that the 8000 server is telling the browser: "don't look here, go to the 3000 server for your answer".

If that is your intent then you can send a redirect by

smartMirror.get("/launch", (req, res) => {
    res.redirect("http://somehost:3000/differentUrl");
})

So you might have code such as

smartMirror.get("/launch", (req, res) => {
    // assuming that you can figure out that the 3000 server is running
    if ( the3000ServerIsRunning ) {
         let originalURL = req.originalUrl;
         
         let redirectedURL = // code here to figure out the new URL
         res.redirect("http://somehost:3000" + redirectedURL);
    else {
          // send a local respons
    }
    
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

I think you can do it with the location http response header.

res.location('example://...')
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda