Home > Software design >  How can I redirect the user to a custom URL scheme with Express.js and node-fetch?
How can I redirect the user to a custom URL scheme with Express.js and node-fetch?

Time:03-31

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.

CodePudding user response:

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
    }
    
})

CodePudding user response:

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

res.location('example://...')
  • Related