官术网_书友最值得收藏!

Running the Node application

Now that we have our app.js file, the only thing left to do is to run it, and we'll do that over in Terminal. Now, to run this program, we have to navigate into our project folder. If you're not familiar with Terminal, I'll give you a quick refresher.

You can always figure out where you are using pwd on Linux or macOS, or the dir command on Windows. When you run it, you'll see something similar to the following screenshot:

I'm in the Users folder, and then I'm in my user folder, and my user name happens to be Gary.

When you open Terminal or Command Prompt, you'll start in your user directory.

We can use cd to navigate into the desktop, and here we are:

Now we're sitting in the desktop. The other command you can run from anywhere on your computer is cd /users/Gary/desktop. And this will navigate to your desktop, no matter what folder you're located in. The command cd desktop, requires you to be in the user directory to work correctly.

Now we can start by cd-ing into our project directory, which we called hello-world, as shown in the following command:

cd hello-world

With the following screenshot:

Once we're in this directory, we can run at the ls command on Linux or Mac (which is the dir command on Windows) to see all of our files, and in this case we just have one, we have app.js:

This is the file we'll run.

Now, before you do anything else, make sure you are in the hello-world folder and you should have the app.js file inside. If you do, all we'll do is run the node command followed by a space so we can pass in an argument, and that argument will be the filename, app.js as shown here:

node app.js

Once you have this in place, hit enter and there we go, Hello world! prints to the screen, as shown here:

And that is all it takes to create and run a very basic Node application. While our app doesn't do anything cool, we'll be using this process of creating folders/files and running them from Terminal throughout the book, so it's a great start on our way to making real-world Node apps.

主站蜘蛛池模板: 邓州市| 阿克| 杭锦后旗| 民乐县| 内黄县| 东丽区| 绥棱县| 昌图县| 锦屏县| 鲁甸县| 沛县| 布拖县| 巩留县| 兴业县| 牙克石市| 徐汇区| 松溪县| 珠海市| 重庆市| 榆中县| 奇台县| 景宁| 泾川县| 丰都县| 稻城县| 冷水江市| 平远县| 高淳县| 雷州市| 东海县| 延边| 湛江市| 阿鲁科尔沁旗| 义马市| 徐汇区| 新沂市| 苍溪县| 嵊泗县| 察雅县| 鄢陵县| 旬阳县|