CLI Tools for Node.js Development
The next section of this course is about CLI tools. Command-line interface tools. Typically, we install them globally but again that's not all the way the case. I mentioned before that sometimes it's a good idea to install a CLI locally. Here we go. node-dev tool. node-dev: we don't really worry that we might have different versions than our teammates.
So node-dev, most of the times it's okay to have it globally, to have a little bit older or a little bit newer version than someone else. Why? It's mostly a development tool. You would monitor your code and node-dev would monitor and restart your node automatically when there are some file changes. Otherwise you would have to manually restart your server every time you make a change and it's wasting too much of your time which is better spent on the actual writing of the code. So node-dev—use this tool. node-static. So node-static will serve your files over the HTTP protocol. It's a static web server similar to Apache httpd or Nginx. Of course this is no a real production server, so don't use it in production, but it's great for development because it's pretty fast and it's pretty easy to install, just "npm install -g node-static" And then it doesn't have too many configurations. So whatever folder you started it, in that the folder would be the root of your webserver. So there's no need to go and mess around with configurations. Apache httpd has a lot of configurations. I spend a lot of hours doing that. With node-static I don't spend anytime configuring it. It's just static that a command. "static ." to run it in the current folder and I'm good to go. I have my files on the localhost port 8080. :-)
The next tool is Node Inspector. Node inspector, it's a Chrome Google DevTools and they were they would be listening, so the same interface as in the dev tools that interface would be listening for the node process when it's run in the debug mode, so you can debug your code with node inspector and you would get a nice dev tools interface, graphical interface. Docker. Docker! I'm including it with this Node toolchain because it's a great pair with Node, especially Alpine. We use Alpine Docker image which is a stripped-down version of Ubuntu, we use it in production for at Capital One for micro services! So Node micro services and Docker, they are great, great, great combination.
What about curl?! So curl comes built-in on every Linux system and macOS, but for Windows you can get it too. Click on the link and you can get it for Windows. Why I like curl: curl allows me to make HTTP requests to test my web applications. It's very low level, so I can inspect my request especially if it's HTTPS or SSH and I can see what is happening what is going on with that request before writing my test. Maybe I can just quickly do a curl especially both with OAuth. With OAuth you have to make those requests, so it's good to also use curl has kind of a verification that "yes my request is actually working". All I need to do is just provide a special encrypted signature in the header or authorization key in the token or bearer in the header of my request so with curl I can do all that and it's a good verification that my application is working or my Node code is giving me the right token.
And yeah, what an NVM will do for you it will allow you to have multiple node versions. You install them once and then you can quickly switch between them without having to install and re-install. So nvm, Node version manager, that's for any serious Node development, you must have it because in different projects you will be sooner or later you'll be using different versions of Node.js so you would need to switch between them. nvm will allow you to do that.
Wintersmith. So my blog, one of my blogs, my personal blog, it's powered by Wintersmith. Why? Because I don't really need anything fancy and I can use Markdown and it's build on Node.js. It's a Node.js tool which will take Markdown, my templates and it will generate a lot of HTML. And then it can just deploy it on the static website static web sites such as Amazon AWS S3 or I'm even using Github pages which is a free service. So Wintersmith, it's a great, great tool for static website generation.
pm2- it's a production tool and it will scale vertically. What is vertical scaling? It's scaling Node on a particular server on a particular virtual machine. So pm2 will increase the number of processes it will make your application and your system more fault tolerant and zero time reload meaning they need to restart something you would have multiple other processes still handling the load. So pm2, you can use it with Docker, inside of the docker as well there is "pm-docker", and you can also use it for development. There is "pm2-dev". So instead of node-dev you can use pm2. It's a good alternative. So pm2, your production process manager. Let's take a look how pm2 works. So "pm2 start server". I'm starting my server and also once, so once I start my server it goes into background and then to bring it back I can look at this table so now my curl is working that means I have the server running. So "pm2 ls", that's what brings by this table that has all the processes So "pm2 stop all", I can finish all of them or you can finish them individually. and every time you need a command so "pm2 help", that's the list of commands. Let's take a look at it again. So every process would have a PID. I can also give a name such as app name and by using the name I can terminate some processes, I can also have processes from different different files not just from one file. I can also do that and they would be listening on different ports. So pm2, highly, highly recommended. It's a real production process manager