Node js throw exception in events

I am wondering how to throw an error or returning value from event. My code looks like this: _initConnection(){ try{ const errorValidation = this.errorValidation const HOST = “192.168.2.32” const PORT = 8282 this.socket = net.createConnection(PORT, HOST) this.socket.on(‘data’, function(data) { errorValidation(data)// throwing error console.log(‘Received: ‘ + data); return data }); this.socket.on(‘err’, function(err) { console.log(‘Connection’+err); }); this.socket.on(‘close’,… Read More Node js throw exception in events

My POST request is returning a 404 not found but my GET request on the same route works

I’m currently following along https://youtu.be/Ud5xKCYQTjM but in TypeScript, my GET request works GET http://localhost:48080/users HTTP/1.1 200 OK X-Powered-By: Express Content-Type: application/json; charset=utf-8 Content-Length: 2 ETag: W/"2-l9Fw4VUO7kr8CvBlt4zaMCqXZ0w" Date: Thu, 28 Jul 2022 13:14:52 GMT Connection: close [] while my POST request fails POST http://localhost:48080/users Content-Type: application/json { "name": "Kyle", "password": "password" } HTTP/1.1 404 Not Found… Read More My POST request is returning a 404 not found but my GET request on the same route works

Mapping data form database to an array of objects in JS

I have this code in App.js const getPlayers = async()=>{ const players = await API.getPlayers(); setPlayers(players) } getPlayers() This code in my API.js file const getPlayers = async () => { return getJson( fetch(SERVER_URL + ‘users’, { credentials: ‘include’}) ).then( json => { return json.map((user) => { return { id: user.id, name: user.name, rank: user.rank… Read More Mapping data form database to an array of objects in JS

Use middlewares conditionally – Cannot set headers after they are sent to the client

I’m trying to make a POST request where I need to switch between two middlewares based on a req.body parameter. The two middlewares are auth middlewares for customer and merchant respectively. I send a type in request body and its value can be either customer or merchant. The merchantAuth and customerAuth middlewares are almost alike… Read More Use middlewares conditionally – Cannot set headers after they are sent to the client