Lesson 01-Introduction to Node.js Fundamentals

Creating a Server

Native Node

Basic Process

  • Import the http module and create a server instance.
  • Bind the server to a port for listening.
  • Complete the response. The connection must be closed to finalize the request process; otherwise, the client will not display the data.

Data Response

Set response headers: res.setHeader('Content-type', 'text/html;charset=utf-8')
Retrieve GET/POST request parameters:

  • GET: url.parse(req.url, true)
  • POST:
let params = '';
req.on('data', (data) => {
  params += data;
});
req.on('end', () => {
  console.log('POST request parameters:', qs.parse(params));
});

Write data:

fs.readFile(__dirname + '/example.html', 'utf-8', (err, data) => {
if (err) {
  res.write('HTML request error');
  res.end();
} else {
  res.write(data);
  res.end();
}
});

Close the connection: res.end()

const http = require('http');
const fs = require('fs');
const url = require('url');
const qs = require('querystring');

// Create server instance
const server = http.createServer();
// Bind port for listening
server.listen(8080, (err) => {
  if (err) {
    console.log(`Server failed to start: ${err}`);
  } else {
    console.log('Server started successfully, open http://localhost:8080');
  }
});
// Complete response process
server.on('request', (req, res) => {
  // res.setHeader('Content-type', 'text/html;charset=utf-8');
  // Check request method
  if (req.method === 'GET') {
    // Retrieve GET request parameters (from query string)
    console.log('GET request parameters:', url.parse(req.url, true));

    // Check request path
    if (req.url === '/') {
      fs.readFile(__dirname + '/example.html', 'utf-8', (err, data) => {
        if (err) {
          res.write('HTML request error');
          res.end();
        } else {
          res.write(data);
          res.end();
        }
      });
    } else {
      fs.readFile(__dirname + '/example.jpg', (err, data) => {
        if (err) {
          res.end('Image request error');
        } else {
          res.end(data);
        }
      });
    }
  } else if (req.method === 'POST') {
    // Retrieve POST request parameters (from request body)
    let params = '';
    req.on('data', (data) => {
      params += data;
    });
    req.on('end', () => {
      console.log('POST request parameters:', qs.parse(params));
    });
    res.end();
  }
});

Express

Basic Process

  • Create an Express instance.
  • Set up routes.
  • Start listening on a port.

Data Response

  • Additional operations are provided here; refer to the official documentation for detailed APIs.
  • Promisify readFile to avoid callback hell.
  • Request headers: req.headers
  • Request body: req.body
// app.js: Implement CRUD operations for user data
const express = require('express');
const dbOption = require('./db');

const app = express();

// Configure request data types
app.use(express.urlencoded());
app.use(express.json());

// Read (GET)
app.get('/', async (req, res) => {
  try {
    const result = await dbOption.getData();
    res.send(result);
  } catch (err) {
    res.status(500).json(err);
  }
});

// Create (POST)
app.post('/', async (req, res) => {
  let body = req.body;
  if (req.body) {
    // Auto-increment ID
    let userList = await dbOption.getData();
    body.id = userList.users.length + 1;

    // Append request data to existing data
    userList.users.push(body);
    try {
      const addError = await dbOption.writeData(userList);
      if (!addError) {
        res.status(200).json({
          msg: 'Added successfully'
        });
      } else {
        res.status(500).json({
          error: addError
        });
      }
    } catch (err) {
      res.status(500).json({
        error: err
      });
    }
  } else {
    res.status(403).json({
      error: 'Missing user information'
    });
  }
});

// Update (PUT)
app.put('/:id', async (req, res) => {
  // Find user by ID
  let userList = await dbOption.getData();
  const userID = Number.parseInt(req.params.id);
  const user = userList.users.find(item => item.id === userID);

  if (user) {
    // Update user information
    let body = req.body;
    user.username = body.username || user.username;
    user.age = body.age || user.age;
    userList.users[userID - 1] = user;

    // Write updated data
    const addError = await dbOption.writeData(userList);
    if (!addError) {
      res.status(200).json({
        msg: 'Updated successfully'
      });
    } else {
      res.status(500).json({
        error: addError
      });
    }
  } else {
    res.status(403).json({
      error: 'User does not exist'
    });
  }
});

app.listen(8080, (err) => {
  if (err) {
    console.log(`Server failed to start: ${err}`);
  } else {
    console.log('Server started successfully, open http://localhost:8080');
  }
});

// db.js: Data operations
const fs = require('fs');
const { promisify } = require('util');

// Promisify read/write operations to avoid callback hell
const readFile = promisify(fs.readFile);
const writeFile = promisify(fs.writeFile);

exports.getData = async () => {
  const data = await readFile(__dirname + '/db.json', 'utf-8');
  return JSON.parse(data);
};

exports.writeData = async (data) => {
  return await writeFile(__dirname + '/db.json', JSON.stringify(data));
};

// db.json
{
  "users": [
    {
      "id": 1,
      "username": "Monica",
      "age": 18
    },
    {
      "id": 2,
      "username": "Kaka",
      "age": 12
    },
    {
      "username": "TaoLoading",
      "age": "22",
      "id": 3
    }
  ],
  "video": []
}

Koa

Context

Koa encapsulates Node’s request and response objects into a single Context object. Each request creates a new context, which is referenced in middleware as the receiver or ctx identifier, as shown below:

app.use(async ctx => {
 ctx // is the Context
 ctx.request // is a Koa Request
 ctx.response // is a Koa Response
});

Note: Bypassing Koa’s response handling (e.g., using Node’s res.end()) is not supported.

Onion Model

Routing
Implement routing architecture: Use @koa/router.
Parameter handling:
– Dynamic parameters: ctx.params
– Query parameters: ctx.query
– Body parameters: Use koa-body

Error Handling
Throw exceptions with throw: ctx.throw(400, '400 Error')
Handle exceptions using error event listeners.

// Koa using Router
const Koa = require('koa');
const Router = require('@koa/router');
const { koaBody } = require('koa-body');

const app = new Koa();
const router = new Router();
app.use(koaBody());

// Route prefix
router.prefix('/api');

router.get('/user', ctx => {
  ctx.body = 'user';
});

router.get('/getParams/:id', ctx => {
  // Dynamic parameters
  console.log('Dynamic parameters', ctx.params);
  // Query parameters
  console.log('Query parameters', ctx.query);
  ctx.body = 'GET parameters';
});

router.post('/postParams', ctx => {
  // Body parameters
  console.log('Body parameters', ctx.request.body);
  ctx.body = 'POST parameters';
});

router.get('/throw', ctx => {
  ctx.throw(400, '400 Error');
  ctx.body = 'throw';
});

router.get('/error', ctx => {
  JSON.parse('');
  ctx.body = 'error';
});

app.use(async ctx => {
  ctx.body = 'Hello';
});

// Error handling
app.on('error', (err, ctx) => {
  console.log('Error:', err);
  ctx.body = err;
});

app.use(router.routes());

app.listen(8081, (err) => {
  if (err) {
    console.log(`Server failed to start: ${err}`);
  } else {
    console.log('Server started successfully, open http://localhost:8081');
  }
});
// Koa Onion Model
const Koa = require('koa');

const app = new Koa();

const middleware1 = (ctx, next) => {
  console.log('Middleware 1 start');
  next();
  console.log('Middleware 1 end');
};

const middleware2 = (ctx, next) => {
  console.log('Middleware 2 start');
  next();
  console.log('Middleware 2 end');
};

// Output: Middleware 1 start, Middleware 2 start, Middleware 2 end, Middleware 1 end
/* app.use(middleware1);
app.use(middleware2); */

const middleware3 = (ctx, next) => {
  console.log('Middleware 3 start');
  next();
  console.log('Middleware 3 end');
};

const middleware4 = async (ctx, next) => {
  await console.log('Middleware 4 start');
  next();
  await console.log('Middleware 4 end');
};

const middleware5 = (ctx, next) => {
  console.log('Middleware 5 start');
  next();
  console.log('Middleware 5 end');
};

// Output: Middleware 3 start, Middleware 4 start, Middleware 3 end, Middleware 5 start, Middleware 5 end, Middleware 4 end
// Due to async middleware4, the call stack ends after Middleware 3, so "Middleware 3 end" is executed first
app.use(middleware3);
app.use(middleware4);
app.use(middleware5);

app.listen(8081, (err) => {
  if (err) {
    console.log(`Server failed to start: ${err}`);
  } else {
    console.log('Server started successfully, open http://localhost:8081');
  }
});

Event Loop

  • Node.js is a single-process, single-threaded application, but it achieves high performance due to the asynchronous callback interfaces provided by the V8 engine, which handle large amounts of concurrency.
  • Nearly all event mechanisms in Node.js are implemented using the observer pattern from the design patterns.
  • The single-threaded nature of Node.js operates like a while(true) event loop, continuing until no event observers remain. Each asynchronous event generates an event observer, and when the event occurs, the corresponding callback function is invoked.

In the event-driven model, a main loop listens for events and triggers callback functions when events are detected. This process is simple and resembles the observer pattern: events act as subjects, and all registered handlers function as observers.

Node.js has several built-in events, which can be bound and listened to by importing the events module and instantiating the EventEmitter class:

// Import events module
const events = require('events');
// Create eventEmitter object
const eventEmitter = new events.EventEmitter();
// Create event handler
const connectHandler = function connected() {
  console.log('Connection successful.');
  // Trigger data_received event
  eventEmitter.emit('data_received');
};
// Bind connection event handler
eventEmitter.on('connection', connectHandler);
// Bind data_received event with an anonymous function
eventEmitter.on('data_received', function () {
  console.log('Data received successfully.');
});
// Trigger connection event
eventEmitter.emit('connection');

console.log('Program execution completed.');

EventEmitter

  • All asynchronous I/O operations in Node.js emit an event to the event queue upon completion.
  • Many objects in Node.js emit events: a net.Server emits an event for each new connection, and an fs.readStream emits an event when a file is opened. All objects that emit events are instances of events.EventEmitter.
addListener(event, listener) // Adds a listener to the listener array for the specified event
on(event, listener) // Registers a listener for the specified event, accepting an event string and a callback
once(event, listener) // Registers a one-time listener that is removed after being triggered once
removeListener(event, listener) // Removes a specific listener for the specified event; the listener must be registered

const EventEmitter = require('events').EventEmitter;
const event = new EventEmitter();

// Listener #1
const listener1 = function listener1() {
  console.log('Listener listener1 executed.');
};

// Listener #2
const listener2 = function listener2() {
  console.log('Listener listener2 executed.');
};

// Bind connection event with listener1
event.addListener('connection', listener1);

// Bind connection event with listener2
event.on('connection', listener2);

let eventListeners = event.listenerCount('connection');
console.log(`${eventListeners} listeners listening to connection event.`);

// Trigger connection event
event.emit('connection');

// Remove listener1
event.removeListener('connection', listener1);
console.log('listener1 is no longer listened to.');

// Trigger connection event again
event.emit('connection');

eventListeners = event.listenerCount('connection');
console.log(`${eventListeners} listeners listening to connection event.`);

console.log('Program execution completed.');

Global Objects

The global object (global) encompasses all global variables (except global itself) as its properties. In Node.js, we can directly access global properties without needing to include it in the application.

require(id)

  • Built-in modules are loaded directly from memory.
  • File modules are located through file lookup.
  • Packages are located via the main field in package.json.

module.exports

(function (exports, require, module, __filename, __dirname) { // Wrapper head
}); // Wrapper tail

JSON Files

  • Loaded using fs.readFileSync().
  • Parsed using JSON.parse().

Loading Large Files

  • Files are cached after being required.
  • Heavy usage can lead to large amounts of data residing in memory, causing frequent garbage collection and memory leaks.

module.exports and exports

Execution

(function (exports, require, module, __filename, __dirname) { // Wrapper head
  console.log('hello world!') // Original file
}); // Wrapper tail

exports

  • exports is a property of module, defaulting to an empty object.
  • Requiring a module returns its exports property.
  • exports.xxx exports an object with multiple properties.
  • module.exports = xxx exports a single object.

Usage

// module-2.js
exports.method = function () {
  return 'Hello';
};

exports.method2 = function () {
  return 'Hello again';
};

// module-1.js
const module2 = require('./module-2');
console.log(module2.method()); // Hello
console.log(module2.method2()); // Hello again

Global Variables

// Output the value of the global variable __filename
console.log(__filename);
// Output the value of the global variable __dirname
console.log(__dirname);
path.join(__dirname, 'views', 'view.html'); // Use path.join to avoid manual handling of slashes
// Home directory
const homeDir = require('os').homedir();

console

PlaceholderTypeExample
%sStringconsole.log(‘%s’, ‘value’)
%dNumberconsole.log(‘%d’, 3.14)
%jJSONconsole.log(‘%j’, { name: ‘Chenng’ })

View PATH

node

console.log(process.env.PATH.split(':').join('\n'));

Set PATH

process.env.PATH += ':/a_new_path_to_executables';

Get Information

// Get platform information
process.arch // x64
process.platform // darwin

// Get memory usage
process.memoryUsage();

// Get command-line arguments
process.argv

nextTick

The process.nextTick method allows you to place a callback at the head of the next event loop queue, enabling delayed execution that is more efficient than setTimeout.

const EventEmitter = require('events').EventEmitter;

function complexOperations() {
  const events = new EventEmitter();

  process.nextTick(function () {
    events.emit('success');
  });

  return events;
}

complexOperations().on('success', function () {
  console.log('success!');
});
Share your love