DevOps.dev

Devops.dev is a community of DevOps enthusiasts sharing insight, stories, and the latest development in the field.

Follow publication

Getting Started with Node.js as a Backend: A Beginner’s Guide

Anuja Desale
DevOps.dev
Published in
3 min readDec 28, 2023

If you want to start exploring backend technologies, then Node.js is a fantastic choice. Node.js allows you to use JavaScript on the server side, providing a seamless experience if you’re already familiar with JavaScript for frontend development. Lets understand basics of Node.js and how to use it as a backend technology.

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime built on the V8 JavaScript engine. It allows you to run JavaScript code outside of a web browser, making it ideal for server-side development. Node.js is known for its non-blocking, event-driven architecture, making it efficient for handling concurrent operations.

Installing Node.js

Before you start, you need to install Node.js on your machine. You can download the latest version from the official website: Node.js Downloads. Follow the installation instructions for your operating system. Once installed, you can check the Node.js version and npm (Node Package Manager) version in your terminal:

node -v
npm -v

Your First Node.js Script:

Let’s create a simple Node.js script to print “Hello, Node.js!” to the console. You can create the file at any location in your system, e.g. create a new folder MyNodeWorkspace. create a file named myfirstapp.js in the folder and add the following code to the file:

//myfirstapp.js
console.log('Hello, Node.js!');

Save the file and run it using the terminal:

node myfirstapp.js

You should see the message “Hello, Node.js!” printed to the console as below.

Sample output from VSCode editor terminal

Congratulations, you’ve just run your first Node.js script!

Building a Simple Backend Server

Now, let’s take a step further and create a basic HTTP server using Node.js. We’ll use the built-in http module for this. Create a file named myserverapp.js with the following code:

//myserverapp.js
const http = require('http');

const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, Node.js Server!');
});

const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});

Run your server:

node myserverapp.js

Visit http://localhost:3000 in your browser, and you should see "Hello, Node.js Server!" as below.

NPM and Packages:

NPM is the package manager for Node.js, allowing you to install and manage packages (libraries and tools) for your projects. Create a package.json file in your project's root directory:

npm init -y

This will generate a basic package.json file. Now, you can install packages using:

npm install package-name

For example, let’s install the express framework, a popular choice for building web applications:

npm install express

Update your myserverapp.js file to use Express:

const express = require('express');
const app = express();
const PORT = 3000;

app.get('/', (req, res) => {
res.send('Hello, Express!');
});

app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});

Run your server:

node myserverapp.js

Visit http://localhost:3000 to see "Hello, Express!" as below.

We’ve learned how to run a Node.js script, create a basic HTTP server, and use the Express framework.

As we delve deeper, we’ll discover the vast ecosystem of Node.js and its rich community, offering numerous libraries and frameworks.

Reference:

  1. https://nodejs.org/en/learn/getting-started/introduction-to-nodejs
  2. https://www.w3schools.com/nodejs/default.asp

Published in DevOps.dev

Devops.dev is a community of DevOps enthusiasts sharing insight, stories, and the latest development in the field.

Written by Anuja Desale

BeautifulSoul$$FunLoving$&LoveTravel$$Sportive$$TechEnthusiast

No responses yet

Write a response