Node.js Get Started
- Go to https://nodejs.org
- Download the LTS (Long Term Support) version
- Run the installer and follow the instructions
Open your terminal/command prompt and type:
node --version
npm --version
You should see version numbers for both Node.js and npm (Node Package Manager).
If the commands don't work:
- Restart your terminal/command prompt
- Make sure Node.js was added to your system's PATH during installation
- On Windows, you might need to restart your computer
Once you have installed Node.js, let's create your first server that says "Hello World!" in a web browser.
Create a file called myfirst.js and add this code:
let http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
Save the file on your computer, for example:
C:\Users\Your Name\myfirst.js
This code creates a simple web server. When someone visits your computer on port 8080, it will show "Hello World!"
Node.js files must be initiated in your computer's Command Line Interface (CLI).
How to open the CLI depends on your OS:
- Windows: Press the Start button and search for "Command Prompt" or type "cmd" in the search field
- Navigate to the folder that contains
myfirst.js. Your CLI should look something like:
C:\Users\Your Name>_
Start your CLI, type the following command, and hit Enter:
node myfirst.js
Now your computer works as a server! Anyone who accesses your computer on port 8080 will see "Hello World!"
Open your browser and type:
http://localhost:8080