2
const express = require('express');
          app = express();
          PORT = 3030;
    
    app.use(express.json());
    app.use(express.urlencoded({ extended: false }));
    
    app.get("/", (req, res) => {
      res.send('Hello World')
    });
    
    app.post('/', (req, res) => {
      console.log(req.body);
      res.json(req.body)
    })
    
    app.listen(PORT, err => {
      if(err) {
        return console.log('ERROR', err);
      }
    
      console.log(`Listening on port ${PORT}, http://localhost:${PORT}/`)
    });

I'm using Postman to test it here is what I'm using.

I'm using form-data:

{number: 123, new: new}

I've tried using body-parser but it's depreciated.

I'm using express "^4.17.1"

I'm just wondering if it's something local that could be causing the problem because I've looked up a couple of tutorials and they don't have this issue.

postman details

I've also tried making a request using fetch:

let options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      number: '123',
      new: 'new'
    })
  }
  fetch('http://localhost:3030/', options)
    .then(response => response.json())
    .then(data => console.log(data));

all I get back is:

test.html:61 POST http://localhost:3030/ net::ERR_CONNECTION_REFUSED

Uncaught (in promise) TypeError: Failed to fetch

  • 1
    Similar question here: [How to get data from form body](https://stackoverflow.com/questions/56758241/node-js-express-how-to-get-data-from-body-form-data-in-post-request) – jfriend00 Jun 18 '21 at 01:42

1 Answers1

3

Your postman image shows form-data (which is probably multipart/form-data) as the content-type which is not something that either of your middleware handlers knows about.

express.json() parses JSON content-types like application/json.

express.urlencoded() parses URl encoded content-types like application/x-www-form-urlencoded.

People often use multer for parsing multipart/form-data, but if this data is just simple form data (name/value pairs), then you should encode it as either application/x-www-form-urlencoded or application/json rather then deploy multer. Multer is typically used when you have file uploads and that's when multipart/form-data is a necessary choice.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I've also tried using a fetch request but I get: POST http://localhost:3030/ net::ERR_CONNECTION_REFUSED Uncaught (in promise) TypeError: Failed to fetch – kealanheena Jun 18 '21 at 01:49
  • @kealanheena - Just change the content-type in Postman to something you have middleware installed for. Presumably, your `fetch()` usage had some problem because all it does is send an http request, same as Postman. We can't help you with the `fetch()` issue without a lot more info added to your question about that specific circumstance and error. – jfriend00 Jun 18 '21 at 01:52
  • I know it works when I use x-www-form-urlencoded – kealanheena Jun 18 '21 at 01:56
  • @kealanheena - Well, it won't work with `multipart/form-data` unless you install Express middleware that knows how to parse that content-type (like multer). That's what I wrote in my answer. Is there more to your question than that? – jfriend00 Jun 18 '21 at 01:59
  • 1
    @kealanheena - If this answered your question, you can indicate that to the community by clicking the checkmark to the left of the answer and that will also earn you a few reputation points for following the proper procedure here. – jfriend00 Jun 18 '21 at 02:18