NEW
Font size
WorksheetsNode.js and Express Fundamentals
Total questions: 20
Worksheet time: 10mins
What is Node.js primarily used for?
Developing mobile games
Creating desktop applications
Designing static websites
Building scalable network applications and web servers.
Which of the following is a core module in Node.js?
http
path
events
fs
What does Express.js provide for Node.js applications?
A command-line tool for managing Node.js packages
A template engine for rendering HTML
A database management system for Node.js
Express.js provides a web application framework for Node.js, enabling routing, middleware, and easy handling of HTTP requests.
What is middleware in Express?
Middleware is a front-end framework
Middleware in Express is a function that processes requests and responses.
Middleware is a programming language
Middleware is a type of database
How do you install Express in a Node.js project?
Execute 'npm install express.js' in your project directory.
Run 'npm install express' in your project directory.
Use 'npm add express' in your project folder.
Run 'npm start express' in your project directory.
Which method is used to read a file synchronously in Node.js?
fs.readFileAsync
fs.readFile
fs.readFileSync
fs.readSync
What is the purpose of the 'fs' module in Node.js?
The 'fs' module is for creating web servers in Node.js.
The 'fs' module is used for file system operations in Node.js.
The 'fs' module is used for network operations in Node.js.
The 'fs' module handles database connections in Node.js.
How do you define a route in Express?
app.get('/path', (req, res) => { res.send('Hello World!'); });
app.get('/anotherPath', (req, res) => { res.send('Hello World!'); });
app.route('/path', (req, res) => { res.send('Goodbye World!'); });
app.post('/path', (req, res) => { res.send('Hello World!'); });
What is the default port for an Express application?
3000
5000
8080
4000
What command is used to start a Node.js application with nodemon?
nodemon
npm run
nodemon start
node
How can you serve static files in an Express application?
Set 'app.staticFiles('path/to/static/files');' to serve static files.
Implement 'app.serveStaticFiles('path/to/static/files');' to serve static files.
Use 'app.get('/static', express.static('path/to/static/files'));' to serve static files.
Use 'app.use(express.static("path/to/static/files"));' to serve static files.
What is the purpose of the 'app.use()' method in Express?
To define routes in an Express application.
To mount middleware functions in an Express application.
To handle HTTP requests directly.
To serve static files in an Express application.
How do you handle errors in Express middleware?
Ignore errors and continue processing requests.
Use an error-handling middleware function with four parameters (err, req, res, next) to handle errors in Express.
Use console.log to display errors in the terminal.
Return a generic error message without details.
What is the function of the 'req' and 'res' objects in Express routes?
The 'req' object sends the response, while the 'res' object receives the request.
The 'req' object is used for logging, and the 'res' object is for error handling.
The 'req' object manages database connections, while the 'res' object formats HTML.
The 'req' object handles the request data, while the 'res' object manages the response to the client.
How can you read a JSON file using the 'fs' module?
const fs = require('fs'); fs.readFile('file.json', 'utf8', (data) => { console.log(data); });
const fs = require('fs'); fs.readFile('file.json', 'utf8', (err, data) => { if (err) throw err; const jsonData = JSON.parse(data); console.log(jsonData); });
const fs = require('fs'); fs.readFile('file.json', (data) => { console.log(data); });
const fs = require('fs'); fs.readFileSync('file.json');
What is the purpose of the 'body-parser' middleware in Express?
To handle routing in Express applications.
To serve static files in Express applications.
To manage database connections in Express.
To parse incoming request bodies in Express applications.
How do you define a GET route in Express?
app.delete('/route', (req, res) => { res.send('Response'); });
app.put('/route', (req, res) => { res.send('Response'); });
app.get('/route', (req, res) => { res.send('Response'); });
app.post('/route', (req, res) => { res.send('Response'); });
What is the command to install nodemon globally?
npm install nodemon -g
npm install --global nodemon
npm install nodemon
npm install -g nodemon
How can you send a JSON response in Express?
Use res.write({ key: 'value' });
Use res.send({ key: 'value' });
Use res.json({ key: 'value' });
Use res.sendFile({ key: 'value' });
What is the significance of the 'db.json' file in a Node.js application?
The 'db.json' file is used for logging application errors.
The 'db.json' file is significant as it acts as a mock database for storing application data in JSON format.
The 'db.json' file serves as a configuration file for server settings.
The 'db.json' file is a template for generating HTML pages.
