Take Your First Step Into A Larger World
To Begin Again
Setting Up The Server
To get started, go to https://nodejs.org/en/ and download Node.js. Once installed we can get our server up and running in a few easy steps.
First, create a new .js file and copy this code into it.
The first line is an example of a Node.js module. Modules are basically JS libraries that come built-in to Node. To use a module you must use the require() function along with the name of the desired module. When we require ‘http’, the application now has access to the http module which allows us to create our server.
Lines 3 through 6 calls the createServer() function on the http module variable declared above. We want to establish what our response will be for our app. As long as the response code is 200, we can access our app whose content type has been defined (text/html) and what the actual content will be (“This is my new Node App.”). The last line dictates what port to listen for (8080).
Running the Code
In order to run our code we need the Command Line Interface. Our file won’t be able to be used until we initialize it. Using the CLI, navigate to the folder containing the file. Once there, initialize the code by typing node followed by the file name.
Now our file has been initialized. Navigate to the specified port (8080) and you will see the content of the app. In this case its simple text.
Simple But Effective