A
A
artiqt2021-02-26 00:48:11
Express.js
artiqt, 2021-02-26 00:48:11

How to make a POST request on my hosting?

There is a server that runs on my PC server.js:

const express = require("express");
const cors = require("cors");
const userRoutes = require("./routes/routes");
const app = express();

app.use(express.json());
app.use(cors());

app.use("/api", userRoutes); //   /api/user/add

app.listen(process.env.PORT || 5000, () => {
    console.log(`server is running on ${process.env.PORT || 5000} PORT`);
});


the front is uploaded to the hosting, and the request looks something like this:
axios.post("http://localhost:5000/api/user/add", { ...values });

Everything works, but the problem is that with such a request, data is added to the database only from my PC, it is not added from other devices.

If I change the request to "domen.com/api/user/add" , I get a 404 error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2021-02-26
@artiqt

in process.env.PORT the port other than 5000 is accidentally not defined?
also try to write universal listening address in app.listen(port,'0.0.0.0',()=>...)
instead of localhost write 127.0.0.1

and here I misunderstood the question

axios.post("localhost:5000/api/user/add", { ...values });
localhost же, это не адрес твоей машины а локальный адрес где запущен код... пиши свой домашний ip, настраивай роутер на перенаправление внешних для него запросов внутрь локальной домашней сети и помни про российских провайдеров, которые создают случайные проблемы для поднятия серверов локально (например порт 8080 может не работать а к примеру 81 будет)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question