Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

node.js - I am having problem using http-proxy-middleware

I have two servers running in my backend, because of that I have to use the http-proxy-middleware package but I am encountering some problems.

This is my code in the frontend which is running on localhost:3000

axios("/api2/login",data)
    .then((res) => {
});

This is my code in the backend which is running on localhost:5001

const { createProxyMiddleware } = require('http-proxy-middleware');

app.use(createProxyMiddleware('/api2', {target: 'http://localhost:5001', changeOrigin: true}))

app.post("/login", (req, res, next) => {
    res.send("Logged In");
});

This code is no working showing this error in the browser's console

GET http://localhost:3000/api2/login 404 (Not Found)
Uncaught (in promise) Error: Request failed with status code 404
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.handleLoad (xhr.js:61)

I am not able to understand where I am going wrong.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like it's hitting localhost:3000 instead of localhost:5001 which is where your server is running from.

axios("http://localhost:5001/api2/login",data)
    .then((res) => {
});

You can also set the baseURL in axios. HTTP get request with Axios gets sent with the local host IP at the beginning of the URL


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.6k users

...