NEW
Font size
WorksheetsNode.js Basics for Students
Total questions: 10
Worksheet time: 5mins
What is Node.js?
Node.js is a JavaScript runtime for server-side execution.
Node.js is a programming language for mobile apps.
Node.js is a database management system.
Node.js is a front-end framework for web design.
How do you create a new Node.js project?
Execute 'node create' in your command line.
Run 'npm start' in your terminal.
Run 'npm init' in your terminal.
Type 'npm install' to set up the project.
Which command is used to install a package in Node.js?
npm install
npm add
npm get
node install
What is the purpose of the 'require' function in Node.js?
To execute scripts in the browser environment.
To define global variables in Node.js.
To create new files and directories in the system.
To import modules and use their functionalities in Node.js.
How do you start a Node.js server?
const express = require('express'); const app = express(); app.listen(3000);
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/'); });
const server = require('net').createServer(); server.listen(8080);
const http = require('http'); http.createServer().listen(4000);
What is npm in the context of Node.js?
npm is a database for Node.js.
npm is a framework for building Node.js applications.
npm is a web server for Node.js.
npm is the package manager for Node.js.
How can you read a file in Node.js?
const fs = require('fs'); fs.readFile('path/to/file.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); });
const fs = require('fs'); fs.writeFile('path/to/file.txt', 'Hello World!', (err) => { if (err) throw err; });
const fs = require('fs'); fs.unlink('path/to/file.txt', (err) => { if (err) throw err; });
const fs = require('fs'); fs.appendFile('path/to/file.txt', 'New data', (err) => { if (err) throw err; });
What is the difference between synchronous and asynchronous code in Node.js?
Synchronous code allows parallel execution; asynchronous code blocks execution.
Synchronous code blocks execution; asynchronous code allows non-blocking execution.
Synchronous code is faster; asynchronous code is slower in execution.
Synchronous code uses callbacks; asynchronous code does not use any callbacks.
How do you handle errors in Node.js?
Use try-catch for synchronous code, callbacks for error handling in async code, and .catch() for promises.
Use console.log to debug errors in all cases.
Ignore errors and let the application crash.
Always return null for error handling in async functions.
What is the role of the package.json file in a Node.js project?
The package.json file manages server configurations and environment variables.
The package.json file is used for styling and layout in a Node.js project.
The package.json file defines project metadata, dependencies, and scripts in a Node.js project.
The package.json file stores user credentials and passwords.
