I want to make an API that takes HTTP Requests body and sends that body with the TCP server. I'm using Express and Net library for Node.JS.
So I have a file was named MakeAnnounce.js. Inside that file, code takes the "announce" value from the body of the request and write it to the database. Then it needs to write that value to the TCP server. TCP Server is in another file. But I don't know how can I call it. Because "socket.write" needs to be in something, like "server.on('connection', function (socket)". How can make it public? Like where ever I want to use the "socket.write" function.
I tried to "module.export" but it didn't work. Also, I tried to declare the "socket" function outside of the "server" but I'm unable to do that.
const db = require('quick.db');
module.exports = (req, res) => {
const duyuru = req.body;
const sinif = req.get("sinif");
if(db.get('token').includes(req.get("token") === false)) {
res.status(401).send({message: "You are not authorized to use this route"});
}
else if(!duyuru || !sinif) {
res.status(400).send({message: "One or more parameters are missing"});
}
else {
db.push(`duyuru.${sinif}`, duyuru);
res.status(200).send({message: "Duyuru added successfully"});
require('../../server/index.js')(duyuru);
// This is where I want to send "duyuru" parameter to TCP server. Like,
// socket.write(`New data arrived ${duyuru}`)
db.delete(sinif);
db.push(sinif, duyuru);
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/jC63uEl
Comments
Post a Comment