DEV Community

dark gaming
dark gaming

Posted on

Node.js for Beginners: A Simple Guide to Kickstart Your Development Journey

Node.js is the backend for both your frontend and backend needs. It has everything you need, and trust me, its features are powerful enough to blow you away.

The Node.js CLI is a very powerful interface, giving you access to npm (Node Package Manager), the world's largest library manager, with over 1.3 million packages available. But enough with the technical talk! What can Node.js really do for you? Does it remind you of the first time you heard the word JavaScript? Well, it might.

Let me be your guide and show you how to view Node.js as the tool you’ll want to rely on for your development needs.

The first interesting thing about Node.js is that it can create web servers. This means that, in addition to your frontend script, you'll have something else running on the backend. But wait, doesn’t that make things more complex? It does, but it helps you solve bigger problems, and trust me, it’s only hard the first time.

The support your frontend needs is what a web server can provide. You can now build your own APIs to handle tasks. But how much can they handle? That depends on your needs. Let’s say you need to store user credentials in a database. Where do you store them? LocalStorage? Not ideal—it's less secure and more complex to manage. What about using a Map or Set? No, that’s not great either. It wouldn't scale for each user.

So, what’s the solution?

Great question. What if you had something that runs 24/7 and is only accessible by you or your PC? That’s exactly what a web server can do. They’re built for this, and they can handle much more. Now, imagine setting up a Map or Set on that server. Does that spark your interest? If yes, you’re in the right place. You’ll be amazed by how simple it can be, and there are tons of resources to explore. If not, stick with me until the end, and you’ll see how cool this is.

Let's Look at a Simple Web Server Example:

const http = require('http');

// Yes, imports are done this way in Node.js

const server = http.createServer((request, response) => {
  if (request.url === '/' && request.method === 'GET') { 
    // '/' is the default endpoint for your API
    response.writeHead(200, { "Content-Type": "text/plain" });
    response.end("Hello, it's Zee!");
  }
});

server.listen(3000, () => {
  console.log(`Server is running at http://localhost:3000`);
}); // You can always define a custom port
Enter fullscreen mode Exit fullscreen mode

The web server is pretty underrated, but what if you need to handle different HTTP methods (like POST, PUT, DELETE)? Writing all of this from scratch would result in a lot of code. The key is to make it short and simple while still keeping the functionality intact.

Now, let’s talk about APIs. APIs are small parts of code that each handle their own logic. Let me show you an easy and short example.


Introducing Express: Simplifying Backend Development
Express is a lightweight but powerful framework that simplifies backend development. With Express, you can quickly build APIs without writing excessive code. Here’s how easy it is to make an API with Express.

Step 1: Install Express
Run the following command in your CLI to install Express:

npm install express
Enter fullscreen mode Exit fullscreen mode

Step 2: Create Your API

const express = require('express');

// Initialize the Express app
const app = express();
const PORT = 3000;

app.get('/', (req, res) => {
  res.status(200).send("Hello, it's Zee again!");
});

app.listen(PORT, () => {
  console.log(`Server is running at http://localhost:${PORT}`);
}); 
Enter fullscreen mode Exit fullscreen mode

See how simple that is? That’s it! You’ve made your first API. If you actually tried this out, congrats! You should definitely try it yourself.

However, remember: to ensure your requests reach your server, you might need to use CORS (Cross-Origin Resource Sharing). What is CORS? You’ll have to figure that out on your own. Consider this a little cliffhanger.

Top comments (0)