Node-Inspector

Summary

  1. Show me the code
  2. Man in the middle
  3. Node-Inspector
  4. Pipe

Session

1. Show me the code

This is the what all the developers say when they want to know how good or what a person is working on.

This came up because I was explaining how this week I had done the nodeschool.io course.

2. Man in the middle

We talked a little bit about what is this concept, because all our connexions goes through different computers over the world, and with this technique hackers use to steal data.

To avoid this problems we use HTTPS protocol so the person who is in the middle can only see encrypted data.

3. Node-Inspector

To debug with NodeJS code, we have to use this tool Node-Inspector that when used, we can simply adding debugger; to our code, we’ll make it stop and open an inspector on our Browser.

This would be the process:

For this example we are going to use an exercise of learnyounode, where any data passed to the code, is returned in capital letters.

This is the code for the exercise:

var http = require('http')
var map = require('through2-map')
var server = http.createServer(function(req, res) {

  req.pipe(map(function(chunk) {
    return chunk.toString().toUpperCase()
  })).pipe(res)
})
server.listen(process.argv[2])

To start the server we are going to run the following command, passing as a second argument the port of the server.

$ node http_uppercaserer.js 3000

this will open the server and will let it waiting for us to work on it.

then we are going to pass to the sever some data with the following command:

$ curl --data AirCrew http://localhost:3000

this will automatically return AirCrew

But if we would like to test the code with breaks, we do the following:

Now we are going to add a debugger; in line 4

var http = require('http')
var map = require('through2-map')
var server = http.createServer(function(req, res) {
  debugger;
  req.pipe(map(function(chunk) {
    return chunk.toString().toUpperCase()
  })).pipe(res)
})
server.listen(process.argv[2])

now we can start the application with the following command:

$ node --debug-brk http_uppercaserer.js 3000
debugger listening on port 5858

in other tab of the terminal we use to run the code the command:

$ node-inspector 
Node Inspector v0.10.1
Visit http://127.0.0.1:8080/debug?ws=127.0.0.1:8080&port=5858 to start debugging.

then we open the browser on the link (remember to press command when click on iterm to go to the page.) and once is loaded, we press play to start debugging.

Then we can send as we did previously our command with the data so we’ll see the debugger on our browser stopping where we specified so we can start debugging like we would normally do on a normal javascript code executed on the browser instead of NodeJS.

A shorter and seems simple way of doing this would be the following:

Before continue we should do a killall node to be sure that there is no other activity running.

So, to simple launch the code with the debugger tool on the default browser, we would have to use the following:

$ node-debug http_uppercaserer.js 3000

This will launch the code and simply open the debugger view on the browser, once is loaded we would press play (F8) to start the debugger and then we’ll simple send the data like we did before

$ curl --data AirCrew http://localhost:3000

4. Pipe

So, Pipe it’s a really important tool that not a lot of people knows how to use, it is important because it reduces the size used.

On big files, the use of pipe is mandatory.

in a popular way, to know what pipe does, we can say that it takes a file or data, start a process with the first information that get from that file or data and as soon as get the first results, starts sending the results, it doesn’t wait for the file to be totally loaded to start or to get the results of the process done in all the data.

In a normal process without pipe, if we try to work on a file that is 1 GB large, we would have to upload the file completely to the server and then once is uploaded start processing the information.

we can get more information on the NodeJS API

Remarks:

  • using the key ESC in google chrome shows the console.
  • At iTerm using cmmd + click into a link, open the browser with the link.

Write your code like the maintainer is going to be a Serial Killer.

Homework.

  • TDD – James Shore
    • Unit testing small functions.
  • Work on the project (future portfolio)
Thanks for your time @aircrewmentor, this has been a good session. I feel like I am understanding a lot of things. Still a lot of work ahead.
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s