D
D
DashaPoliazzz2022-04-18 00:19:47
Node.js
DashaPoliazzz, 2022-04-18 00:19:47

Why is null arriving?

Hello. I created the simplest server, GET requests work, but there is some kind of trouble with POST.

Server code:

const express = require("express");
const cors = require("cors");

const app = express();

const PORT = process.env.PORT || 80;

app.listen(PORT, () => {
  try {
    console.log(`Server started on ${PORT}...`);
  } catch (e) {
    console.error(`${e} - app.listen`);
  }
});

const userList = [];
const userData = [];

app.get("/userList", (req, res) => {
  try {
    res.send(userList);
  } catch (e) {
    console.error(`${e} - app.get("/userList")`);
  }
});

app.get("/userData", (req, res) => {
  try {
    res.send(userData);
  } catch (e) {
    console.error(`${e} - app.get("/userData")`);
  }
});

app.post("/userData", (req, res) => {
  try {
    userData = req.body;
  } catch (e) {
    console.error(`${e} - app.post("/userData")`);
  }
});

app.post("/userList", (req, res) => {
  try {
    userList.push(req.body);
  } catch (e) {
    console.error(`${e} - app.post("/userList")`);
  }
});

app.use(cors());


Application code with POST request:

const axios = require("axios");
const postUrl = "https://stormy-gorge-63462.herokuapp.com/userList";
axios.post(postUrl, {
  firstName: "Fredd",
  lastName: "Flint",
});


But for some reason, I catch on the server every time null is added to the array.

625c84b579d93030286401.png

Tell me, please, what is the problem?

I apologize in advance for such an amateurish approach, this is my first attempt at working with express and nodeJs

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question