Are you curious about node.js, but don't know where to start? Many of the resources out there provide too much detail too soon, which can be confusing. It's like learning how to change spark plugs when you just want to take a test drive.
If you want a simple "Hello, world." example, here you go. These steps will get you up and running without superfluous explanation.
Prerequisite - Install node.js
There are a variety of ways to install node and they're all self-explanatory. If you haven't finished this step already, come back when you're done. To test if you're set up, type this in your terminal:
node -v
If you get a version returned, you're ready.
Step 1 - Create a file named server.js.
This isn't Apache or IIS, so create this file wherever you want. Wherever it is, that will be your server's root.
Step 2 - Copy the code below into server.js.
Each line has a comment to describe what it's doing.
Step 3 - Start your app.
In the terminal, navigate the directory containing server.js. Type the following to start your new app:
node server.js
You will see the message indicating the server is ready to handle responses.
Step 4 - Hello, world.
In your browser, navigate to http://localhost:3000. If you're too nerdy to use a browser, use cURL in another terminal. VoilĂ , you're running the most spartan node.js server ever.
Step 5 - Again, with feeling.
Now you can move on with your life if you only wanted a cursory introduction. Alternatively, if you want to start playing around, stop the server (ctrl+c
twice in the terminal), edit server.js, and start it again. The example code is not ideal by any stretch of the imagination, just meant to be easy and clear.
When you're ready to really start digging in, check out this book, which is the friendliest intro I've found. You can also jump right into the api if you're the guy who was using cURL earlier.
No comments:
Post a Comment