node.js client side errors not showing in console

546 Views Asked by At

I am on a school Chromebook, where developer tools are disabled. I sometimes coded on node.js, having server-side and client-side code. But, as developer tools are disabled, I can't check errors on the client-side, as to only find that it sometimes just stops working without a clue as to what is wrong. I have had this issue a whole lot whenever coding client-side code.

How could I detect and identify the error with only access visually to the node.js console, as well as express and socket.io?

For example,

const express=require("express");
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.use(
  '/client', 
  express.static(__dirname + '/client')
);
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/client/index.html');
  console.log("sending site");
});
http.listen(3000, () => {
  console.log('listening on *:3000 (http.listen)');
});

io.on('connection', (socket) => {
  socket.emit("ERRORNOW",26);
});
//"<script src="/socket.io/socket.io.js"></script>" assumed to be in HTML file
var socket=io();

socket.on("ERRORNOW",()=>{
  if("this doesnt have an ending curlybracket){

  //} 
  //this is the error it doesnt have the ending curly bracket, 
  //but it doesn't show in the node.js console 
  //(at least on browser IDEs like repl.it), 
  //and debugging without the developer tools 
  //can be infuriating to say the least
});

The question is How can I identify a node.js client-side error on a web-based IDE without developer tools?

1

There are 1 best solutions below

0
Kitty Craft0 On

I have already had this issue a while ago, and due to the lack of developer tools on school Chromebooks, found the solution on my own, but I just thought that maybe I should also put it here.

My solution, as simple as it may be, is to just use a try-catch statement and send the error to the node.js console. It took a while to figure that out...

So, if you don't already have it, you would need a function that when triggered from the client side can log something into the console, inside of the io.on("connection",()=>{}); thing, like so:

io.on("connection",()=>{
  socket.on("log", input=>{
    console.log(input);
  });
});

For example, if everything runs off of a single function that is triggered really fast (specific, but relevant to me, as I make web games), or just off of something, you can run it off of a function inside a try catch, like so:

//"<script src="/socket.io/socket.io.js"></script>" assumed to be in HTML file
var socket=io();
try{
socket.on("ERRORNOW",()=>{
  try{
  if("this doesnt have an ending curlybracket){

  //} 
  }catch(error){
    socket.emit("log",error);
  }
});
}catch(error){
  socket.emit("log",error);
}

So now, with this simple solution, you can stop commenting out 90% of your code to find that one error that makes it stop working while breaking it further by accidentally commenting out parts that help it to work in the first place!