skip to Main Content

EDIT: We are now pretty sure it could be the .htaccess redirecting the POST because in the virtual server in cpanel we can POST correctly using curl, is this a thing. This is our .htaccess

RewriteBase /
RewriteRule ^index.html$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.starrynights.co.nz/$1 [R,L]

EDIT: I am using express 4.17.1

EDIT: So far with the help of the below I have tried:

  • adding bodyparser
  • using post man to send through x-www-form-urlencoded
  • installing multer and sending through form.data

All have come back with undefined when using postman


I am trying to POST to an express server however I cannot get my req.body.message to come back with the data, it only comes back as undefined.

This is deployed on cpanel and is using the Node.js application,
enter image description here

This is the code installed on it, it works when using localhost, however will not work deployed?

const express = require("express");
const cors = require("cors");
const router = express.Router();
const app = express();

app.use(cors());
app.use(express.json());
// app.use(express.urlencoded({ extended: true }));
app.use("/", router);

app.listen(3030, () => console.log("Server on 3030"));

router.post("/send_mail", (req, res) => {
    res.send(`the message is ${req.body.message}`);
    }
);

Post Man POST

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    So we managed to figure out the problem, We were using our htaccess to force https but were posting to the http which resulted in the data being redirected.

    We changed the POST to only go to the https and it worked perfectly.

    EDIT: it broke again, but we found out if hosting using apache and cpanel, it will put a "/" at the end of the POST. So when posting the address must be https://www.example.com/send_mail/ otherwise it wont POST the data


  2. I tried your code "as is" and it runs well. Try using following command from windows terminal:

    curl -i -X POST -H "Content-Type:application/json" -d "{"message": "Hello World!" }" http://localhost:3030/send_mail
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search