100===Dev Ops/Node

The Essential Node.js Guide for Developers

블로글러 2024. 6. 7. 18:06

The organized summary will now include more details and code examples for each section.

Essential Node.js Guide for Developers

1. Introduction to Node.js

  • What is Node.js?
    • Definition: Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine.
    • Usage: Designed for building scalable network applications.
    • Architecture: Non-blocking, event-driven architecture makes it efficient and suitable for data-intensive real-time applications.

2. Setting Up Node.js

  • Installation
    • Using Official Website: Download the installer from nodejs.org and follow the installation steps.
    • Using nvm (Node Version Manager):
      # Install nvm
      curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
      # Install Node.js
      nvm install node
      # Verify Installation
      node -v
      npm -v

3. Core Concepts

  • Modules
    • Built-in Modules: Examples include fs for file system operations, http for creating servers.
    • const fs = require('fs'); fs.readFile('file.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); });
    • Creating and Importing Custom Modules:
    • // math.js exports.add = (a, b) => a + b; // app.js const math = require('./math'); console.log(math.add(2, 3)); // 5
  • Event Loop
    • Definition: Handles all asynchronous callbacks.
    • Example:
      setTimeout(() => {
        console.log('This will run after 1 second');
      }, 1000);
  • NPM (Node Package Manager)
    • Installing Packages:
      npm install express
    • Managing Dependencies with package.json:
      {
        "name": "my-app",
        "version": "1.0.0",
        "dependencies": {
          "express": "^4.17.1"
        }
      }

4. Building a Simple Server

  • HTTP Module
    • Creating a Basic Server:
      const http = require('http');
      const server = http.createServer((req, res) => {
        res.statusCode = 200;
        res.setHeader('Content-Type', 'text/plain');
        res.end('Hello, World!\n');
      });
      server.listen(3000, '127.0.0.1', () => {
        console.log('Server running at http://127.0.0.1:3000/');
      });

5. Working with Express

  • Introduction to Express.js
    • Definition: Minimalist web framework for Node.js.
    • Features: Simplifies route handling and middleware integration.
  • Setting Up Express
    • Installation:
      npm install express
  • Creating an Express App
    • Basic Setup:
    • const express = require('express'); const app = express(); const port = 3000; app.get('/', (req, res) => { res.send('Hello, World!'); }); app.listen(port, () => { console.log(`Server running at http://localhost:${port}/`); });

6. Middleware and Routing

  • Middleware Functions
    • Definition: Functions executed during the lifecycle of a request to the server.
    • Usage:
      app.use((req, res, next) => {
        console.log('Request URL:', req.originalUrl);
        next();
      });
  • Routing
    • Defining Routes:
      app.get('/users', (req, res) => {
        res.send('User List');
      });
      app.post('/users', (req, res) => {
        res.send('Add User');
      });

7. Error Handling

  • Handling Errors in Express
    • Using Middleware:
      app.use((err, req, res, next) => {
        console.error(err.stack);
        res.status(500).send('Something broke!');
      });

8. Connecting to Databases

  • MongoDB with Mongoose
    • Setting Up:
    • const mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/mydatabase', { useNewUrlParser: true, useUnifiedTopology: true }) .then(() => console.log('MongoDB connected')) .catch(err => console.log(err)); const userSchema = new mongoose.Schema({ name: String }); const User = mongoose.model('User', userSchema); const newUser = new User({ name: 'Alice' }); newUser.save().then(() => console.log('User saved'));

9. Real-time Communication

  • Socket.IO
    • Basic Usage:
    • const http = require('http'); const socketIo = require('socket.io'); const server = http.createServer((req, res) => { res.writeHead(200); res.end('Hello, World!'); }); const io = socketIo(server); io.on('connection', (socket) => { console.log('a user connected'); socket.on('disconnect', () => { console.log('user disconnected'); }); }); server.listen(3000, () => { console.log('Server running at http://localhost:3000/'); });

10. Testing and Debugging

  • Using Mocha and Chai for Testing
    • Installation:
    • npm install mocha chai --save-dev
    • Example Test:
    • const assert = require('chai').assert; describe('Array', function() { it('should return -1 when value is not present', function() { assert.equal([1, 2, 3].indexOf(4), -1); }); });
  • Debugging with Node.js
    • Using Built-in Debugger:
      node inspect server.js
    • Using nodemon for Auto-restart:
      npm install -g nodemon
      nodemon server.js

Reference

For a more detailed guide, please visit the original article on The Essential Node.js Guide for Developers.

728x90

'100===Dev Ops > Node' 카테고리의 다른 글

Prisma Introduced  (0) 2024.06.12
How does the non-blocking I/O model in Node.js improve performance?  (0) 2024.06.06
NodeJS Introduced  (0) 2024.06.06