Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to call REST API inside node js with relative url?

I have code in my express controller like this :

const http = require('axios');

module.exports = {
    login: (req, res) => {
        res.render("admin/login.ejs", { errorMessage: req.flash('message')[0] });
    },
    processLogin: (req, res) => {
        axios.post(`/api/login`, {
                "email": req.body.email,
                "password": req.body.password
            })
            .then(function(response) {
                console.log(response);
            })
            .catch(function(error) {
                console.error(error);
            });
    }
}

This path "/api/login" API url is defined in express route in same application. But, it gives me me error like this :

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:372:5)
    at URL.onParseError (node:internal/url:553:9)
    at new URL (node:internal/url:629:5)
    at dispatchHttpRequest (D:\projects\calvarycomz.com\node_modules\axios\dist\node\axios.cjs:2394:20)    
    at new Promise (<anonymous>)
    at http (D:\projects\calvarycomz.com\node_modules\axios\dist\node\axios.cjs:2330:10)
    at Axios.dispatchRequest (D:\projects\calvarycomz.com\node_modules\axios\dist\node\axios.cjs:3260:10)  
    at Axios.request (D:\projects\calvarycomz.com\node_modules\axios\dist\node\axios.cjs:3610:33)
    at Axios.httpMethod [as post] (D:\projects\calvarycomz.com\node_modules\axios\dist\node\axios.cjs:3649:19)
    at Function.wrap [as post] (D:\projects\calvarycomz.com\node_modules\axios\dist\node\axios.cjs:27:15) {  input: '/api/login',
  code: 'ERR_INVALID_URL'
}

I know the error occurs because the prefix hostname (http://localhost:30000) is not included in the axios endpoint. How can I make an http request using axios just including the url path?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try to create an axios instance and specify the baseUrl for it.
You should then be able to specify only the paths for your requests with:

const axios = require('axios');

const axiosInstance = axios.create({
  baseUrl: 'http://localhost:3000'
})

module.exports = {
  login: (req, res) => {
    res.render('admin/login.ejs', { errorMessage: req.flash('message')[0] });
  },
  processLogin: (req, res) => {
    axiosInstance
      .post(`/api/login`, {
        email: req.body.email,
        password: req.body.password,
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.error(error);
      });
  },
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading