How To Get Started With Nodejs

 

Deploying Node.js apps to production(ish) – Andrew Beeken


Getting started with Node.js involves a few key steps: setting up your environment, learning the basics, and creating a simple application. Here’s a guide to help you get started.

1. Install Node.js and npm

Windows

  1. Download the Installer: Go to the Node.js download page and download the Windows installer.
  2. Run the Installer: Follow the installation steps. The installer also installs npm (Node Package Manager).

macOS

  1. Homebrew: If you have Homebrew installed, you can use it to install Node.js.
    bash
    brew install node

     

  2. Download the Installer: Alternatively, download the macOS installer from the Node.js website.

Linux

  1. Using Package Manager: Depending on your distribution, you can use a package manager. For example, on Ubuntu:
    bash
    sudo apt update sudo apt install nodejs npm
  2. Node Version Manager (nvm): This is a popular method to install Node.js on any OS.
    bash
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash source ~/.nvm/nvm.sh nvm install node

2. Verify Installation

To verify that Node.js and npm are installed, open a terminal or command prompt and type:

bash
node -v npm -v

You should see version numbers for both.

3. Basic Usage

Create a new directory for your project and navigate into it:

bash
mkdir my-node-app cd my-node-app

Initialize a New Node.js Project

bash
npm init -y

This command creates a package.json file with default settings.

Create a Simple Application

  1. Create an index.js file:

    const http = require('http');
    
    const hostname = '127.0.0.1';
    const port = 3000;
    
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('Hello, World!n');
    });
    
    server.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });
    

     

  2. Run Your Application:

    bash
    node index.js

    Open your browser and go to http://127.0.0.1:3000/ to see "Hello, World!".

4. Learn Node.js Basics

Asynchronous Programming

Node.js uses an event-driven, non-blocking I/O model. Understanding callbacks, promises, and async/await is crucial.

Modules

Node.js uses CommonJS modules. Learn how to import and export modules:

// In a file named math.js
exports.add = (a, b) => a + b;

// In your main file
const math = require('./math');
console.log(math.add(2, 3)); // Output: 5

npm Packages

Learn how to use npm to install and manage packages:

bash
npm install express

5. Build a Simple Application

Using Express.js

Express is a minimal and flexible Node.js web application framework.

  1. Install Express:

    bash
     
    npm install express
  2. Create an Express Application:

    const express = require('express');
    const app = express();
    const port = 3000;
    
    app.get('/', (req, res) => {
      res.send('Hello, Express!');
    });
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`);
    });
    
  3. Run Your Application:

    bash
    node index.js

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

6. Explore Further

  • Documentation: Read the official Node.js documentation.
  • Tutorials: Follow tutorials on sites like MDN Web Docs.
  • Courses: Enroll in online courses on platforms like Udemy, Coursera, or freeCodeCamp.
  • Community: Join Node.js communities on platforms like Stack Overflow, Reddit, and GitHub.

By following these steps, you'll set a solid foundation for working with Node.js and be ready to dive deeper into more advanced topics and projects.