Answer the question
In order to leave comments, you need to log in
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());
const axios = require("axios");
const postUrl = "https://stormy-gorge-63462.herokuapp.com/userList";
axios.post(postUrl, {
firstName: "Fredd",
lastName: "Flint",
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question