Node.js lets you run JavaScript outside the browser. It helps build fast and scalable apps on your computer or server.
What is Node.js in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Node.js
import http from 'node:http'; const server = http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello from Node.js!'); }); server.listen(3000, () => { console.log('Server running at http://localhost:3000/'); });
This example shows how to create a simple web server in Node.js.
Node.js uses modules like 'http' to add features beyond JavaScript's basics.
Examples
Node.js
console.log('Hello, Node.js!');Node.js
import fs from 'node:fs'; fs.readFile('file.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); });
Node.js
import os from 'node:os'; console.log('Your system platform is:', os.platform());
Sample Program
This program creates a simple web server that listens on port 3000. When you open http://localhost:3000/ in your browser, it shows the message 'Hello from Node.js!'.
Node.js
import http from 'node:http'; const server = http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello from Node.js!'); }); server.listen(3000, () => { console.log('Server running at http://localhost:3000/'); });
Important Notes
Node.js uses an event-driven, non-blocking model to handle many tasks efficiently.
It is great for building servers and tools using JavaScript outside the browser.
Summary
Node.js runs JavaScript on your computer or server, not just in browsers.
It helps build fast, scalable apps like web servers and real-time tools.
Node.js uses modules to add many useful features beyond basic JavaScript.
Practice
1. What is Node.js primarily used for?
easy
Solution
Step 1: Understand Node.js purpose
Node.js allows JavaScript to run on computers or servers, not just browsers.Step 2: Compare options with Node.js use
Only running JavaScript outside browsers matches Node.js's main use.Final Answer:
Running JavaScript code outside the browser, like on servers -> Option DQuick Check:
Node.js runs JavaScript outside browsers = A [OK]
Hint: Node.js runs JavaScript outside browsers [OK]
Common Mistakes:
- Confusing Node.js with CSS or HTML tools
- Thinking Node.js is for styling or graphics
- Assuming Node.js only works in browsers
2. Which of the following is the correct way to import a module in Node.js?
easy
Solution
Step 1: Recall Node.js module import syntax
Node.js uses require() function to load modules in common usage.Step 2: Check each option
require('fs'); uses require('fs'); which is correct for Node.js modules.Final Answer:
require('fs'); -> Option BQuick Check:
Node.js modules use require() = A [OK]
Hint: Use require() to load modules in Node.js [OK]
Common Mistakes:
- Using import without setup (common in older Node.js)
- Using include or load which are not valid in Node.js
- Confusing Node.js syntax with browser JavaScript
3. What will the following Node.js code output?
console.log(typeof process);
medium
Solution
Step 1: Understand the 'process' object in Node.js
In Node.js, 'process' is a global object representing the current process.Step 2: Determine the type of 'process'
Since 'process' is an object, typeof process returns 'object'.Final Answer:
'object' -> Option AQuick Check:
typeof process = 'object' [OK]
Hint: Global Node.js objects like process are objects [OK]
Common Mistakes:
- Thinking process is undefined or a function
- Confusing process with a string or other type
- Not knowing process is built-in in Node.js
4. Identify the error in this Node.js code snippet:
const http = require('http')
http.createServer((req, res) => {
res.write('Hello World')
res.end()
}).listen(3000)
console.log('Server running')medium
Solution
Step 1: Review Node.js HTTP server code
The code imports http, creates a server with a callback, writes response, ends it, and listens on port 3000.Step 2: Check for syntax or logic errors
Semicolons are optional in JavaScript; callback parameters are correct; listen can work without callback.Final Answer:
No error; code runs correctly -> Option AQuick Check:
Code is valid Node.js server setup = C [OK]
Hint: Semicolons optional; listen callback not required [OK]
Common Mistakes:
- Thinking semicolons are mandatory
- Assuming listen needs a callback
- Confusing callback parameters as missing
5. You want to build a fast web server that handles many users at once using Node.js. Which feature of Node.js helps achieve this?
hard
Solution
Step 1: Understand Node.js architecture
Node.js uses a single thread with event-driven, non-blocking I/O to handle many connections efficiently.Step 2: Evaluate options for handling many users
Only Its single-threaded, event-driven architecture for handling many connections efficiently describes this event-driven model; others mention incorrect features.Final Answer:
Its single-threaded, event-driven architecture for handling many connections efficiently -> Option CQuick Check:
Node.js event-driven model = D [OK]
Hint: Node.js uses event-driven single thread for many users [OK]
Common Mistakes:
- Thinking Node.js uses multi-threading like Java
- Believing synchronous calls are faster
- Confusing Node.js with styling tools
