I am working on a Producer/Consumer system. The producer generates a random expression of two positive integers "2+2=" sends it to the Consumer and then the Consumer computes and sends the solved expression "2+2=4" back to the Producer
I am stuck at the part where I have to send the solved expression back to the Producer. How would I do that?
My Producer:
var XMLHttpRequest = require('xhr2');
const URL = "http://localhost:5000/"
let firstNumber = Math.floor(Math.random() * 10);
let secondNumber = Math.floor(Math.random() * 10);
const express = require('express');
const app = express();
// excecuting random addition
const finalExpression = firstNumber + "+" + secondNumber + "="
console.log(finalExpression);
var xhr = new XMLHttpRequest();
xhr.open("POST", URL, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
expression: finalExpression
}))
My Consumer:
const express = require('express')
const app = express()
app.use(express.json())
app.post('/', (req, res) => {
console.log(req.body.expression);
arr = req.body.expression.split("")
console.log(parseInt(arr[0]) + parseInt(arr[2]))
// res.send(parseInt(arr[0]) + parseInt(arr[2]))
})
app.listen(5000, () => console.log())
What do I have to do within the consumer to send the solved expression to the producer?
If I uncomment // res.send(parseInt(arr[0]) + parseInt(arr[2]))
I get this result:
express deprecated res.send(status): Use res.sendStatus(status) instead index.js:21:7
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status