0

I'm developing an app using VENoM stack, and in the API I have some middleware like this:

const express = require('express');

const router = express.Router();

require('./routes/orderRoutes')(router);
require('./routes/userRoutes')(router);
require('./routes/ftpRoutes')(router);

module.exports = router;

And I each router has a diferent "path", I mean, to call the API the base URL is https://localhost:8081/api/... And each router starts with a diferent route like /order/... /ftp/... or /user/...

The problem is, that I want to call a GET route from ftpRoutes to orderRoutes like this

router.get('/ftp/importFiles', async function(request, response, next) {
        client.ftp.verbose = true
        try {
            await client.access(ftpTest)
            let files = await client.list('/');
            files = files.map((file) => { return path.join(downloadsPath, file.name) });
            console.log(files);
            if (!fs.existsSync(downloadsPath)) {
                fs.mkdirSync(downloadsPath, { recursive: true });
            }
            await client.downloadToDir(downloadsPath, '/');
            console.log(files)
            request.session.files = files;
        } catch (err) {
            console.log(err)
        }
        client.close()
    })

And from this route, that is http://localhost:8081/api/ftp/importFiles I want to call to http://localhost:8081/api/order/parseOrder. I've tried using some options like:

  • response.redirect('/parseOrder')
  • response.redirect('order/parseOrder')
  • response.redirect('api/order/parseOrder')
  • next('order/parseOrder')
  • Etc...

But I cannot make the redirection works fine, so I've thought to change the /ftp/importFiles request to the order router, but I want to keep it separately. Is there any solution to redirect from one router to another router?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Dozer
  • 17
  • 5
  • Check this out https://stackoverflow.com/questions/39047270/express-call-get-method-within-route-from-another-route – Fahad Subzwari Oct 23 '20 at 07:50
  • Are you trying to execute the code in the other route as part of processing the original route or truly redirect to the other route? Those are not the same two options. Which? – jfriend00 Oct 23 '20 at 07:51
  • I want to call /order/parseFile route from /ftp/importFiles route, so I want to redirect it @jfriend00 – Dozer Oct 23 '20 at 07:52
  • You're still not answering or perhaps understanding the question. Redirecting is not calling the other route and getting the answer. Redirecting is replacing all the functionality of this route with some other route. Calling the other route and getting its results to use in this route is a completely different thing from redirecting. I'm still trying to figure out which you are trying to do: redirect? Or call some other route and use its results in this route to create a new response for this route? – jfriend00 Oct 23 '20 at 07:54
  • A redirect tells the caller that you don't have an answer at this URL so the caller should go get an answer at a different URL. This is NOT calling some other route to get its result. – jfriend00 Oct 23 '20 at 07:54
  • Okey, I misunderstood the question, I want to call the other route and use the results of /order/parseFile @jfriend00 – Dozer Oct 23 '20 at 07:55
  • So, I want to use /ftp/importFiles router just to download some files from an FTP server, and then call /order/parseFile route to parse the files downloaded and then, inside /order/parseFile route I use response.send(order) to send the parsed order to the client-side – Dozer Oct 23 '20 at 08:03
  • Then make the functionality in the other route into a function that takes arguments and returns a result and use that function in both routes. For some reason, when defining routes people forget that they can just factor out common functionality into a Javascript function and call that function in multiple places. You don't need to be making an http request at that low a level just to use the common functionality - in fact that's more work than just factoring the common functionality into a function and calling it from both places. – jfriend00 Oct 23 '20 at 08:03

1 Answers1

0

What you are actually asking to do is not a redirect. A redirect tells the calling client that the URL they requested does not have the resource they want and instead, they should ask for a different resource by sending an http request to a different URL.

That's not what you're trying to do. You are trying to use the functionality from a different route in the processing of the current route. You want to return a result from the current route. There are a couple ways to accomplish that.

  1. Make an HTTP request to your own server. You can literally make an http request to your own web server and get the response from the other route using http.request() or a higher level library such as node-fetch() or got() or axios().

  2. Factor common code into a new function and call it both places. Oou can take the functionality that you want from the other route and factor that functionality into a common shared function that you can just call from both routes that want to use that functionality. Then, rather than make a new http request to your own server, you just call a Javascript function that does the kind of processing you want and get the result and you can use that function wherever you need/want it.

I nearly always recommend factoring common code into a shared function as it's ultimately more flexible.

jfriend00
  • 683,504
  • 96
  • 985
  • 979