Help on using 'webapp' WebApp.connectHandlers.use

#1

I don’t quite understand how this works, the documentation doesn’t really explain what’s going on with recieving the data.

In my js (separate folder from meteor server) I’ve added:

const sendData = {
  url: www.google.com,
  key: 12345
};

const sendDataString = JSON.stringify(sendData);

const xhr = new XMLHttpRequest();

xhr.open("POST", 'http://localhost:3000/request', true);
xhr.setRequestHeader("Content-type", "application/json");

xhr.send(sendDataString);

And then in the meteor server (which is on localhost:3000)

WebApp.connectHandlers.use('/request', (req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*')
    console.log(GET DATA SOMEHOW);
    console.log(bodyParser.json(req)) //trying some random
    
    res.writeHead(200);
    res.end(JSON.stringify({ status: 'ok' }));
});
#2

This thread may help:

1 Like
#4

Hmm, I understand a bit more, I definetly have to use req.body

But now I’m getting: Cannot read property ‘json’ of undefined when i use bodyParser

#5

import bodyParser from 'body-parser';

WebApp.connectHandlers.use("/", bodyParser.json());

if you have it like this, restart the server. Sometimes if you yarn add or npm install a new package, it wont get picked up by meteor properly

3 Likes
#6

It worked!

It was a bunch of problems, one being I was importing { bodyParser } to start

1 Like