Last year I started to use Node.js more and more. Personally I really like it. Using node.js on ChakraCore has some advantages over using V8. I’ve decided to get to know node.js a bit better and start a series of posts about it, beginning from the top.
What is node.js?
Why use ChakraCore?
The fact that node.js can run on ChakraCore isn’t the best reason by itself. But there is another bigger advantage in using ChakraCore:
Time Travel Debugging
Every developer knows about placing breakpoints, hitting them and stepping through the code from there. But what if you are just passed the point that has the issue you want to investigate? You normally would place a breakpoint at another place and rerun your application, bringing it to the same state and trying to hit that breakpoint. Wouldn’t it be great if you didn’t have to go though all of that and could just step backwards? That’s exactly what Time Travel Debugging can do.
Let’s get node.js up and running. The easiest way to get going us by using the node-ChakraCore version switcher, nvs. You can download that here.
After installing nvs go to command prompt or PowerShell and run ‘nvs’. Now you can select a ‘remote’. This is will be the node.js type to use.
After selecting a node.js, you can specify the exact version to use:
Wait for it to download and install…
After it is installed you should be able to request the version with “node –version”.
Getting up and running
Now we have node.js running we could begin developing an application. I’m going to start with a very simple application using express.js.
In a later tutorial I’m going to show you how you can scaffold using express.js, but for now let’s get a simple app running. I always start with creating a new folder, let’s call it ‘NodeDemo’ and ‘cd’ into that.
The use of npm has to be initialized inside the folder we just created run ‘npm init’. You can fill out the questions asked, or just enter through for now, until you’re back at the commandline. This will create a package.json file in which the packages used by the project are stored.
Next, let’s add the package for express.js by running the folling command: ‘npm install express –save’. The –save options makes sure the use of the package is registered into the package.json file.
Last thing to do is use the express.js package in an app. It’s time to start Visual Studio Code. You can do that by running ‘code .’ in the commandline (note the period at the end). Add a file, ‘app.js’ and add the following code.
This small piece of code does a few things. The first part loads express.js and initializes it.
After that it registers an HTTP GET on the root url ‘/’. When an GET is performed the function is called and a response is send to the client.
The last part starts the app listening on port 3000 and logs a message to the console when it’s started.
If you followed along, you should be able to run the app now. Go back to the commandline and type:’node app.js’.
Now you should be able to navigate to http://localhost:3000 and see the app running…
And that’s it.