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. fs
B. server
C. url
D. http

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

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

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

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

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 file system
B. In the response object
C. In the global scope
D. In the request object

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

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

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.log()
D. res.push()

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.status(404)
C. res.setStatus(404)
D. res.statusCode = 404

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

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

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

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

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

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

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

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

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

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

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

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

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

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

17 What is Express.js?

A. A fast, unopinionated web framework for Node.js
B. A front-end library
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. npm install express
B. node install express
C. install express
D. npm get express

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

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

20 How do you initialize an Express application?

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

21 Which Express method is used to handle GET requests?

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

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.send()
D. res.emit()

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

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

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

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

25 Which Express method starts the server listening for connections?

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

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

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

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

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

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

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

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 string
B. An empty object {}
C. null
D. undefined

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

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

31 What is express.Router used for?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A. The front-end framework
B. Hardware drivers
C. The database layer
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 database connection
C. The error object
D. The previous request

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

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

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

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

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

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

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

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

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

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

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

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