Add Login node server before another node js server

Viewed 111

I have created a login module using node server (ejs engine) listening on port 3000. Upon successful login with that module, i want to redirect that to another node server listening on port 2011 (made using jade engine). the redirect is not working properly.

The working of login module is such that it put an axios post request with username and password on a live website and get a successcode in return. There is no registration required as the authentication is done by that live website.

Both the servers(3000 and 2011) are working fine but the redirect is not working

Can I route the other node app(2011) on the same server with "localhost:3000/welcome" domain?

'use strict'
const express=require("express");
const app=express();
const path=require("path");
const port=process.env.PORT || 3000
const pubdir=path.join(__dirname,"/public");
const bodyParser = require('body-parser')
const axios=require("axios");
// const axiosCookieJarSupport = require('axios-cookiejar-support').default;
// const tough = require('tough-cookie');
// const https = require('https');
// const Cookie=require("request-cookies").Cookie;
// const http=require("http");
// axiosCookieJarSupport(axios);
// const request= require("request");
var urlencodedParser = bodyParser.urlencoded({ extended: false })
const offlineroute=require("./router")

app.set("view engine", "ejs");
app.use(express.static(pubdir))
app.get("/",(req,res)=>{
    res.render("index");
})

const url uat="It's a valid url working fine"
const url_uat_auth="Another url working fine"


app.post("/login",urlencodedParser,(req,resp)=>{
const data = {
    username:req.body.user_name,
    password:req.body.user_pass,
    captcha:"121212",
    deviceID: null,
}
const apiData={
    action: "ACCESSTOKEN",
    username: data.username,
    password: data.password,   
    }



    
    axios.post(url_uat, data)
    .then((res) => {
        // console.log(`Status: ${res.status}`);
        console.log('Body: ', res.data);
        // console.log("headers",res.headers);
     
    if(res.data.message=='auth')
    {  
         console.log("Successfully logged in");
        //  app.use("/welcome",offlineroute);

        resp.redirect("localhost:2011"); //Help me here.
        (async ()=>{
            try {
              const response = await axios.get(url_uat_auth, apiData);
              console.log(response.status);
              console.log(response.headers);
            } catch (error) {
              console.log(error.response.body);
            }
          })();
    

    }
    else if(res.data.message==null && res.data.errorCode=='AUTH_9002')
    {
        console.log("Wrong username or password");
    }
    else if(res.data.message==null && res.data.errorCode=='SWEB_9000')
    {
        console.log("Invalid Captcha");
    }  
    }).catch((err) => {
        console.error(err);
    });
})
 
app.listen(port,(req,res)=>{
    console.log(`Listening on Port ${port}` );
});
1 Answers

Try to add the full protocol and port:

resp.redirect("http://localhost:2011");
Related