The Command Prompt
If you’re on a mac, then this is known as the terminal. The commands you’ll learn here can be used on Windows, Mac and Linux systems. Depending on your system, you’ll have to search for your command prompt. If you’re on windows, search for cmd and open up the program you find. For mac users, search for the word terminal.
You should see something similar to this. I’m on windows, but as long as you see something with a black background and white font, then you’re using the command prompt.
Say hello to your new best friend! The command prompt may seem intimidating, but it’s actually quite simple to use. You’ll be using the command prompt often so let’s learn a couple of things you’ll need in order to use Node.js.
You don’t need to know every command known to man, but just a few. The first command we’ll learn is how to change the current drive. By default, the command prompt will use the drive your windows installation is on. To change the drive, you simply type in the drive letter name followed by a colon. Like so.
Very easy right? Now, if you would like to go to a certain directory, then you type in cd followed by the directory name. CD stands for change directory. It’s important to know that you must change drivers before you can access it’s folders and files. If you’re going to save your files in the default directory, then there’s no need to change the drive. Here’s an example of me accessing a folder in the A drive.
If you would like to go back one directory, then type in ../ before the directory name. The last command we’ll learn is the mkdir command. This is short for make directory. Here’s an example of me using the mkdir command.
Create a directory anywhere you want and then go to that directory only using the command prompt. Make sure to name this directory server as we’ll be using this directory to run our node.js files. You can use any drive you’d like. I’ll be using my A drive.
Node.js actually isn’t picky where you save your application files. You can have your application created in your My Documents folder or have it in a folder called WalaWalabingbang. You can even have a Node.js application on different drives. However, I like to keep things relative and therefore, I’ll be storing all my files in a folder called server. Feel free to follow my naming conventions.
Using your favourite text editor, navigate to the directory where your server is created. Create a file called index.js and insert the following code.
If you’re still in the command prompt, then you can execute the command below. With your command prompt, navigate to the folder where the index.js file is. This is very important.