DEV Community

Cover image for Intro to Node.js

Intro to Node.js

Kevin Downs
I'm a Philadelphia based Full Stack Software Engineer. I'm a Linguist turned Full Stack Engineer with a background in Healthcare and Customer Service looking for opportunities as a Junior Developer.
・4 min read

Thank you to everyone that has been following along with me so far on my post-grad journey. This week I decided to take a little dive into Node.js and I hope you'll enjoy continuing to follow along as I walk through the basics of getting set up with Node.

What is Node.js?

The first question you will probably be asking (if you're not familiar with Node) is what exactly is Node.js and why would I want to use it. Let's take a look at the documentation:

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Basically, Node.js is an open source runtime environment used to develop server-side and networking applications. We can write these applications in JavaScript and run them on Windows, OS X, and Linux. We can use Node.js in a variety of applications, including but not limited to: data streaming, JSON APIs, single page applications, etc.

Why Node.js?

Node.js may not always be the right fit, but there are many reasons why you might want to use it for your application. The biggest one of these is that Node.js is asynchronous. This means that it never waits for an API to return data. This, among other reasons, makes Node.js very fast.

Node.js also uses a single threaded model with event looping. This lets the server respond in a non-blocking way as well as increases scalability. It also never buffers, and is completely open source.

Let's Get Started

So now that we know a little bit about Node.js, let's get started. The first thing you will need to do is install it on the machine you are working on. You can find downloads for individual operating systems here. Personally, I am working with Ubuntu in WSL2.

You can find the full documentation for installing Node.js using WSL2 here, but I'll walk you through what worked for me.

  • Open your Ubuntu command line.
// If you need to, install `nvm`
curl -o- | bash

// Install the current release of Node.js 
nvm install node

// Install the latest stable LTS release of Node.js (recommended)
nvm install --lts

// Verify everything
node --version
npm --version

It's Working!

Hopefully there were no issues and everything installed properly. If so, we're ready to start playing around! If not, take a look at the docs for installing on your operating system and check back in when everything is running smoothly.

For our first foray into Node.js, lets set up a simple web server that will display a "Hello World" message.

Create an app.js file in your code editor and we can get to work. We will be using require() to load in the Node.js modules we need along with createServer() to build the server that we will use to read the request and return a response.

First, we want to import the http module which we will use to get everything up and running. To do that we simply use require() and assign it to a variable we can use.

const http = require('http');

Now we use the http variable to call createServer() which will create a server instance. We pass that method a function with request and response parameters. Inside the function we can modify the response to have it return "Hello World" when a request is made to the server.

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');

Lastly, we will need to bind the server to a port of our choosing using listen() and include a console log that will let us know that our server is running and on what port.

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);

Now if you run the server using node app.js you will see a console log telling us where the server is running. If you visit the address in your browser, you should be greeted with a "Hello World" message!

This is the app.js file in its entirety:

const http = require('http');

const hostname = '';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);

We Did It!

It's really that simple to get a server up and running with Node.js. I hope you found this little walk through helpful and as always, I'd love to hear any comments. This was just my first jump into Node.js, so feel free to let me know if anything is inaccurate or if I am misunderstanding any concepts.

If you liked this post, feel free to follow me elsewhere on Twitter, Github, or LinkedIn. Happy Coding!


-Node.js guides
-Node.js Tutorial
-Setting up Node.js on WSL 2

Discussion (0)