Node.Js Crash Course Tutotrial 20 Minutes

TechieTalkAI Blogs by Schogini Systems

Here’s a 20-minute tutorial to help you get started with Node.js, a powerful runtime environment that allows you to run JavaScript outside of a browser, typically for server-side applications. This tutorial assumes you know basic JavaScript, but not Node.js.

1. What is Node.js? (2 minutes)

2. Setting Up Node.js (3 minutes)

Step 1: Install Node.js

  1. Download and install Node.js from nodejs.org. Choose the LTS version for stability.
  2. After installation, check if Node.js and npm are installed: bash node -v # Check Node.js version npm -v # Check npm version

Step 2: Create a Project Directory

Step 3: Initialize a Node.js Project

3. Writing Your First Node.js Script (3 minutes)

Step 1: Create a JavaScript File

Step 2: Write a Simple Script

Step 3: Run the Script

4. Node.js Modules (3 minutes)

Node.js has a module system that allows you to split your code into multiple files and reuse code across your application.

Built-in Modules

Node.js comes with several built-in modules like fs (file system), http, path, and os.

Example: File System (fs) Module

fs.readFile('message.txt', 'utf8', (err, data) => { if (err) { console.error(err); return; } console.log(data); }); `` - Create a filemessage.txtwith some text content. - Run the script again withnode app.js` to see the file's content.

Creating Your Own Module

module.exports = greet; ```

5. Creating a Simple HTTP Server (3 minutes)

Node.js is commonly used to create web servers. Let’s create a basic HTTP server using the built-in http module.

Step 1: Use the http Module

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

server.listen(3000, () => { console.log('Server running at http://localhost:3000/'); }); ```

Step 2: Run the Server

6. Asynchronous Programming and Callbacks (3 minutes)

Node.js uses non-blocking I/O and callbacks to handle asynchronous tasks.

Example: Asynchronous File Read

const fs = require('fs');

console.log('Start reading file...');
fs.readFile('message.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});
console.log('Finished reading file');

7. npm and Installing External Packages (2 minutes)

Node.js comes with npm, which lets you install and manage external libraries and packages.

Step 1: Install a Package

Step 2: Use the Installed Package

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

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

8. Working with JSON and APIs (2 minutes)

Node.js is great for building APIs that serve JSON data.

Example: Sending JSON Response

const http = require('http');

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'application/json');
  res.end(JSON.stringify({ message: 'Hello, JSON!' }));
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});

9. Error Handling (2 minutes)

Handling errors properly is important in Node.js.

Example: Error Handling in File Read

const fs = require('fs');

fs.readFile('non-existent-file.txt', 'utf8', (err, data) => {
  if (err) {
    console.error('Error reading file:', err);
    return;
  }
  console.log(data);
});

10. Final Tips (1 minute)

With this basic understanding, you’re ready to start building simple server-side applications using Node.js!