Debugging server side with node.js - javascript

I have a burning question in my head regarding debugging, you see when I am writing Javascript client side I can go to Chrome's console and track my variables and objects etc to see what is happening with my code better.. I am just not able to get my head around about how can we do the same on the server side (node js)? Let's say my front end submitted a form to my express server, how do I go about checking if for instance the req object even received it or not? where do I go about checking variables and objects (debugging) server side code? I definitely can't do it on console of browser as the code exists and executes on the server side so I can't access server side objects etc through browser's console.

You can still do console.log(). It'll print to the screen where you run the server. However, it's not as good as walking through the code with debugger which you can set breakpoints and do lots of other things debuggers can do. I've used both webstorm's debugger and node-inspector.
You might want to look into node-inspector. The debugger is like Chrome's Dev-Tool, which you might be familiar with. The link below provides everything from installation to tutorials.
https://github.com/node-inspector/node-inspector

Node comes with a REPL (Read-Eval-Print-Loop). It works a bit like the console of chrome but requires a bit of configuration and set up of it's scope.
Here is an example: http://derickbailey.com/2014/07/02/build-your-own-app-specific-repl-for-your-nodejs-app/

if you do console.log to variables and objects you can see it on you command prompt from where you are running your server

The console.log() has 2 kinds:
When it write in the client codes. It will console the message on your brower.
When it write in the server codes. It will console the message on your editor such as Webstorm.
Suggest use debug to check variables and objects instead of using console.log() because it's more convenient.

You might want to consider webstorm. It has advanced debugging support built-in for nodejs which allows you to set breakpoints just like in Chrome's debugging tools.

Related

Is it possible to attach a debugger to k6 scripts?

I'm hoping to adopt k6 for load testing, but I'm having trouble developing scripts for it. My primary use case is to check at each request to see if I'm receiving the correct headers and content and would like to inspect the response with a debugger.
I tried to run the script on its own by attaching the node inspect debugger (https://nodejs.org/api/debugger.html) but the file doesn't get executed because the import and export module keywords are unrecognized by this current version of node (8.7.0)
I'm also unable to find any documentation on how to debug these scripts.
There is no debugger support (currently known) for k6 scripting. It's manual debugging at this time.
k6 runs javascript (ECMA6) and has an API documented at http://k6.io
Sidenote: k6 is not node and will not work with node tooling.
I recently opened an issue about this - the need for a "debug" mode where detailed information about requests is printed to stdout.
https://github.com/loadimpact/k6/issues/331
To be clear, this issue is not about creating a "real" debugger, like gdb or similar, where you can step through script code, but rather a special mode of operation where lots of HTTP request information is being output to stdout in real time, to facilitate understanding exactly what is happening between client and server when your script code is executed.
I will probably try to implement something like this as soon as Emily (the maintainer) merges in some major CLI changes she is working on right now.

can a linux shared library open, display and printf to terminal?

I am creating a Firefox extension that calls a Linux shared library via the js-ctypes mechanism. I want my extension to display debug information on a standard terminal by calling a function in my shared library which then simply calls printf() to display the debug string on a terminal.
However, my application has no terminal. So, is there a way my shared library can open, display and printf() to display such messages?
Yes, I know about the built-in error and browser windows. But for obscure reasons I want to send my debug messages in a terminal window.
So, how can my shared library open and display a terminal to printf() into?
Since you're on Linux anyway, when running Firefox from the command line, it will print some stuff to stdout (or was it stderr) already. Same on MAC. Windows may need the -console switch. Your library that was loaded via js-ctypes could just use printf() and/or fprintf(stderr,...). I seem remember using printf myself when developing js-ctypes stuff in the past.
You can also use dump in JS code. This will only ever dump to the terminal, but not the Browser Console.
Lastly the Components.utils.reportError enables you to print arbitrary messages to th global Browser Console (formerly Error Console). Cu.reportError is basically a shortcut to nsIConsoleService, which will also generate appropriate location information.
You should probably create a simple server that connects to a named pipe and reads data, then prints it to its stdout. It would just be a simple program that connects to a pipe (like a file), and reads from it.
You can use mkfifo to create the named pipe, and your javascript app can write to it, just like you would a file.
A related question is here: How to send a simple string between two programs using pipes?
You can use the command line mkfifo tool, or use the standard C function.

Is there a js console emulator that provides access to the browsers 'real' javascript console?

Does anyone know of any console emulator (preferably made in javascript or jquery) that would allow me direct access to the browsers javascript console that would be found in the developers tools in chrome for example? I have been using jq-console but that only emulates a terminal, very useful so far. However it only emulates a terminal and doesn't provide access to the browsers js console. I need this so that users have a front-end web based access to the console without having to open the developer tools or something similar, so they have access to the variables and objects available in the browsers js console. I had thought of loading the data into the memory of the jq-console instance however i think this would be a cumbersome process and i don't really know how to go about doing it without the information being directly entered into the jq-console instance. any help or guidance would be greatly appreciated. thanks in advance!
direct access to the browsers javascript console
No. Not without opening the console manually, and also you cannot do much more than logging messages. Any emulator will not use the native console.
web-based front end console at CodeAcademy.com
Have you looked at their source code? You can write such one yourself, too. Or have a look at EloquentJavaScript's console script (using Mochi and Codemirror).
a browser console
…is not possible without only emulating it. However, you can use FirebugLite for that, which promises the same look-and-feel as the native Firebug. Also, Opera's Dragonfly is written in JS, and it is released as open source so you might adapt (parts of) it.

Problem running a jQuery application?

I'm developing a jQuery mobile application. The application is developed in JavaScript, jQuery, and HTML. When I debug the application in browser using Firebug (in Firefox) it's working fine, but it doesn't run normally? Is there a reason it's running in debug mode only?
Your question is worded somewhat confusingly so I'm not quite sure what you mean but I think you might be running into a problem with console.XXX() statements in your JS - on a browser that doesn't have console defined (like IE) or a Firefox installation that doesn't have Firebug, console will be undefined and your JavaScript code is likely to fail if you have forgotten to comment out your logging statements. You can verify if that is the problem by commenting out all calls to console (or by watching the console and looking for log output).
A long term solution might be to define the console object when your JavaScript initially loads if it finds that the console doesn't exist - this way, even if you forget to comment out calls to console and someone who doesn't have Firebug installed or is using a different browser tries to use your code, they won't run into this issue. Take a look at this blog post

Command line tool for finding basic Javascript syntax errors?

Are there any command-line Linux tools that can catch basic syntax errors and compile time errors in my Javascript files, even if said Javascript files are written for use in a web browser?
I typically code my Javascript at the same time I'm coding my server side code in say Ruby or Perl. It would save me significant time if I could partially test my client side Javascript the same way I test my server side Ruby and Perl -- on the command line, typically from within emacs. I'm not expecting to catch run time JavaScript errors on the server, just basic things like a mistyped variable name or an extra bracket somewhere or a runaway string, things that could be found before actually attempting to execute the code.
What I do now to test/debug Javascript is the usual cycle of "visit web app in browser; check Firebug or other console; back to emacs to fix errors; repeat." Doing that is certainly unavoidable for more complex types of errors (e.g. involving user and network interaction) but a garden variety syntax error could be caught and dealt with more quickly on the command line without loading up the browser.
I've looked a bit into some server side platforms like node.js, but they all seemed geared toward writing and executing server side code (so all of the client side specific bits in my code would presumably make it barf). I also found an emacs mode for javascript REPL but it doesn't seemed designed to do just basic compile checks - it basically loads the whole page via an external graphical browser and lets you monkey with it, which is precisely what I'm trying to avoid.
Things like YUICompressor effectively do a syntax check too.
This isn't a direct answer to your question as it is a GUI tool, but I'm a big fan of Aptana. It uses SpiderMonkey to compile your code in the background and give you red squigglies for syntax errors as you type. (It also does the same for HTML.) It also tries to give you intellisense for JS, but it is hit-or-miss. It is nice when it works.
Since I probably haven't convinced you to change your development environment, let's answer your question directly. Why not use the SpiderMonkey engine to throw together a command-line app that does what you're looking for? It looks easy enough to plug in. You won't even have to worry about the fact that you're guaranteed to get runtime exceptions (there will be no DOM objects in your environment) — you don't have to actually execute the script. Just call JS_CompileScript and check for success. (And then destroy the JSScript object, of course.)
Or, if you're lazy, you could try Rhino Shell, which is a command-line Java tool which executes JavaScript.
I find JSHint, + its vim plugin are very useful. Light weight of vim and still be able to track the syntax errors of the javascript. JSHint can also be used as a command line tool.
https://github.com/walm/jshint.vim
Javascript debugger with a console in chrome:
The Chrome browser has a javascript debugger can find JavaScript errors:
In Chrome click Tools -> JavaScript Console:
This is examining a JavaScript page with the following code:
var error(){
}
And it tells me what is wrong with it, unexpected '('.
Telling me I can't define a function like that on line 14 of my javascript file.
If you click on the link next to the error, it will take you to and highlight the line that has the error/warning.
I wrote quick-lint-js for this purpose. It points out syntax errors, among other things.
quick-lint-js integrates with several code editors, such as Vim and Visual Studio Code. It also has a UNIX-style command-line utility if you prefer.

Categories