Unit 2: Implementing HTTP Services & Basic Websites With Node.JS - Practice Quiz

INT222 — Advanced Web Development 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which built-in Node.js module is primarily used to create a web server?

A. url
B. http
C. server
D. fs

2 Which method of the HTTP module is used to initialize a server?

A. http.createServer()
B. http.newServer()
C. http.initServer()
D. http.startServer()

3 What arguments does the request listener callback function in http.createServer() accept?

A. (response, error)
B. (data, status)
C. (request, response)
D. (req, data)

4 Which method is used to make the Node.js HTTP server accept connections on a specific port?

A. server.run()
B. server.start()
C. server.listen()
D. server.bind()

5 In a raw Node.js server, where is the request metadata (like headers and URL) stored?

A. In the global scope
B. In the file system
C. In the response object
D. In the request object

6 Which property of the request object holds the part of the URL after the domain name?

A. req.link
B. req.url
C. req.href
D. req.path

7 Which method is used to send a chunk of the response body in the native HTTP module?

A. res.send()
B. res.write()
C. res.push()
D. res.log()

8 What must be called to complete the response and send it to the client in the native HTTP module?

A. res.end()
B. res.finish()
C. res.complete()
D. res.stop()

9 How do you set the HTTP status code to 404 using the native response object?

A. res.code = 404
B. res.setStatus(404)
C. res.statusCode = 404
D. res.status(404)

10 Which method is used to write the status code and response headers simultaneously in native Node.js?

A. res.sendHeader()
B. res.writeHeader()
C. res.setHeader()
D. res.writeHead()

11 What is the correct HTTP status code for a successful request?

A. 400
B. 201
C. 500
D. 200

12 What is the correct HTTP status code for 'Internal Server Error'?

A. 403
B. 500
C. 301
D. 404

13 When sending JSON data using native Node.js, what should the 'Content-Type' header be set to?

A. application/javascript
B. text/json
C. application/json
D. text/html

14 How is basic routing typically implemented in a native Node.js HTTP server?

A. Using if/else statements checking req.url
B. Using switch/case on req.body
C. It handles routing automatically
D. Using a router file

15 Which HTTP method is used to request data from a specified resource?

A. DELETE
B. POST
C. PUT
D. GET

16 Which HTTP method is commonly used to submit data to be processed to a specified resource?

A. HEAD
B. OPTIONS
C. POST
D. GET

17 What is Express.js?

A. A front-end library
B. A fast, unopinionated web framework for Node.js
C. A database for Node.js
D. A strict MVC framework

18 Which command is used to install Express in a Node.js project?

A. node install express
B. npm install express
C. npm get express
D. install express

19 What is the first step to using Express in a file after installation?

A. require('express')
B. start express
C. const app = express()
D. import express from 'node'

20 How do you initialize an Express application?

A. const app = createApplication()
B. const app = express()
C. const app = new Express()
D. const app = start()

21 Which Express method is used to handle GET requests?

A. app.fetch()
B. app.retrieve()
C. app.get()
D. app.read()

22 In Express, what method is used to send a response and automatically set the Content-Type?

A. res.dispatch()
B. res.write()
C. res.emit()
D. res.send()

23 How do you access route parameters (e.g., /users/:id) in Express?

A. req.route
B. req.params
C. req.body
D. req.query

24 How do you access query string parameters (e.g., ?search=term) in Express?

A. req.search
B. req.query
C. req.params
D. req.url

25 Which Express method starts the server listening for connections?

A. app.start()
B. app.init()
C. app.run()
D. app.listen()

26 What is the purpose of 'body-parser' middleware?

A. To send JSON data
B. To parse incoming request bodies
C. To validate the response
D. To parse the URL

27 Before Express 4.16.0, body-parser was a separate install. How do you access JSON parsing now in modern Express?

A. It is not supported
B. express.json()
C. express.body()
D. express.parse()

28 Which middleware parses incoming requests with URL-encoded payloads?

A. express.form()
B. express.raw()
C. express.urlencoded()
D. express.text()

29 If body-parser is not used, what is the value of req.body for a POST request in Express by default?

A. An empty object {}
B. null
C. undefined
D. An empty string

30 What does the extended: true option in urlencoded() middleware do?

A. Enables HTTPS
B. Allows parsing of rich objects and arrays
C. Increases the body size limit
D. Parses XML data

31 What is express.Router used for?

A. To connect to the internet
B. To handle static files
C. To route data to the database
D. To create modular, mountable route handlers

32 How do you expose a router defined in a separate file?

A. export default router
B. return router
C. exports.router = router
D. module.exports = router

33 How do you mount a router module in the main app file?

A. app.use(router)
B. app.mount(router)
C. app.add(router)
D. app.route(router)

34 If you mount a router at '/api', and the router has a route for '/users', what is the full path?

A. /users/api
B. /api
C. /users
D. /api/users

35 Which library is commonly used with Express for validating incoming request data?

A. body-check
B. express-check
C. node-validate
D. express-validator

36 In express-validator, which function is used to validate a field in the request body?

A. checkBody()
B. body()
C. req.check()
D. validate()

37 What does validationResult(req) return in express-validator?

A. The sanitized body
B. An object containing any validation errors
C. The response object
D. Boolean true/false

38 Which method in express-validator is used to check if a field is a valid email?

A. isValidEmail()
B. validateEmail()
C. isEmail()
D. checkEmail()

39 What is the purpose of sanitization in express-validator?

A. To modify input data (e.g., trim whitespace)
B. To reject invalid data
C. To encrypt data
D. To log data

40 Where do you place the validation middleware in an Express route definition?

A. In the package.json
B. Inside the callback function
C. As an array of arguments before the callback
D. In the app.listen() method

41 In express-validator, how do you check if the validation result contains errors?

A. errors.isEmpty()
B. errors.length > 0
C. errors.hasErrors()
D. errors.exist()

42 Which of the following matches a route path for ANY HTTP method in Express?

A. app.all()
B. app.any()
C. app.star()
D. app.every()

43 What is a 'middleware' in the context of Express?

A. The database layer
B. The front-end framework
C. Hardware drivers
D. Functions that have access to req, res, and next

44 What does the next argument represent in an Express middleware function?

A. A callback to pass control to the next middleware
B. The previous request
C. The error object
D. The database connection

45 Which status code indicates 'Bad Request' due to validation failure?

A. 400
B. 503
C. 200
D. 404

46 How do you chain multiple validators on the same field in express-validator?

A. body('field', 'isEmail', 'normalizeEmail')
B. validate('field', [isEmail, normalizeEmail])
C. body('field').isEmail().normalizeEmail()
D. You cannot chain them

47 In a native Node server, how do you differentiate between a POST and a GET request?

A. req.verb
B. req.method
C. req.action
D. req.type

48 What is the primary advantage of using Express over the native HTTP module?

A. It is slower but more secure
B. It removes the need for JavaScript
C. It allows using Java
D. It simplifies routing and middleware management

49 Which method allows you to serve static files (images, CSS) in Express?

A. express.assets()
B. express.files()
C. express.static()
D. express.public()

50 When setting a custom response header in Express, which method is used?

A. res.config()
B. res.set()
C. res.addHeader()
D. res.put()