wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Mastering Express JS Concepts

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.

What is Express JS?

a)

A programming language for web development.

b)

A front-end framework for building user interfaces.

c)

Express JS is a web application framework for Node.js.

d)

A database management system.

2.

How do you install Express JS in a Node.js project?

a)

Use 'yarn add express' instead of npm.

b)

Run 'npm start express' in your project directory.

c)

Run 'npm install express' in your project directory.

d)

Install Express by downloading it from the official website.

3.

What is middleware in Express JS?

a)

Middleware is a tool for managing server hardware in Express JS.

b)

Middleware is a type of database in Express JS.

c)

Middleware in Express JS is a function that processes requests and responses.

d)

Middleware is a front-end framework for building user interfaces.

4.

How can you define a route in Express JS?

a)

app.get('/path', (req) => { return 'Hello World!'; });

b)

app.get('/path', (req, res) => { res.send('Hello World!'); });

c)

app.post('/path', (req, res) => { res.send('Hello World!'); });

d)

app.route('/path', (req, res) => { res.send('Goodbye World!'); });

5.

What is the purpose of the `app.listen()` method?

a)

To connect to a database server.

b)

To start a server and listen for incoming requests on a specified port.

c)

To define middleware functions for the app.

d)

To create a new application instance.

6.

How do you handle GET requests in Express JS?

a)

Use app.post('/route', (req, res) => { res.send('response'); });

b)

app.get('/route', function(req) { return 'response'; });

c)

app.get('/route', (req, res) => { res.json('response'); });

d)

Use app.get('/route', (req, res) => { res.send('response'); });

7.

What is the difference between `req.params` and `req.query`?

a)

`req.params` is for route parameters in the URL path, while `req.query` is for query string parameters.

b)

`req.params` and `req.query` are interchangeable and serve the same purpose.

c)

`req.params` is used for headers, while `req.query` is used for cookies.

d)

`req.params` is for query string parameters, while `req.query` is for route parameters in the URL path.

8.

How can you serve static files using Express JS?

a)

Set up a separate server for static file hosting.

b)

Implement a middleware function to handle static files manually.

c)

Use 'app.get(directoryPath, handler)' to serve static files.

d)

Use 'app.use(express.static(directoryPath))' to serve static files.

9.

What is the role of the `next` function in middleware?

a)

The `next` function allows middleware to pass control to the next middleware function.

b)

The `next` function is used to log requests only.

c)

The `next` function is a method for handling errors.

d)

The `next` function terminates the middleware process.

10.

How do you handle errors in Express JS?

a)

Return a generic error message without details.

b)

Use error-handling middleware with four parameters (err, req, res, next) to handle errors in Express JS.

c)

Ignore errors and continue processing requests.

d)

Use console.log to display errors in the terminal.

11.

What is the purpose of the `body-parser` middleware?

a)

To handle file uploads in a Node.js application.

b)

To serve static files in a Node.js application.

c)

To manage database connections in a Node.js application.

d)

To parse incoming request bodies in a Node.js application.

12.

How can you set up a simple API using Express JS?

a)

Use only HTML and CSS to create the API.

b)

Install Node.js without Express and run it directly.

c)

Install Express, create an app, define routes, and start the server.

d)

Define routes without creating an app instance.

13.

What are the advantages of using Express JS over vanilla Node.js?

a)

Limited plugin ecosystem

b)

Advantages of using Express JS over vanilla Node.js include simplified routing, middleware support, easier request handling, and a rich ecosystem of plugins.

c)

No support for middleware

d)

More complex routing

14.

How do you implement route parameters in Express JS?

a)

Use ':paramName' in the route path and access it via 'req.params.paramName'.

b)

Use 'req.body.paramName' to access route parameters.

c)

Define parameters in the query string instead of the route path.

d)

Use 'req.query.paramName' to retrieve route parameters.

15.

What is CORS and how can you enable it in an Express app?

a)

CORS is a method for encrypting API requests.

b)

You can enable CORS by using the 'express-cors' package.

c)

CORS is a database management system.

d)

To enable CORS in an Express app, install the 'cors' package and use it as middleware: const cors = require('cors'); app.use(cors());