0
0
Node.jsframework~10 mins

CORS configuration in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the CORS middleware in a Node.js Express app.

Node.js
const express = require('express');
const cors = require('[1]');
const app = express();
Drag options to blanks, or click blank then click option'
Abody-parser
Bexpress
Chttp
Dcors
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'express' instead of 'cors'.
Forgetting to install the 'cors' package.
2fill in blank
medium

Complete the code to enable CORS for all routes in the Express app.

Node.js
app.use([1]());
Drag options to blanks, or click blank then click option'
Aexpress
BbodyParser
Ccors
Dhelmet
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express()' instead of 'cors()' as middleware.
Not calling the middleware as a function.
3fill in blank
hard

Fix the error in the CORS options object to allow only 'https://example.com'.

Node.js
const corsOptions = {
  origin: [1]
};
Drag options to blanks, or click blank then click option'
A'https://example.com'
B'*'
C['https://example.com']
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using an array instead of a string for a single origin.
Using '*' which allows all origins.
4fill in blank
hard

Fill both blanks to apply CORS middleware with options and start the server on port 3000.

Node.js
app.use([1](corsOptions));
app.listen([2], () => console.log('Server running'));
Drag options to blanks, or click blank then click option'
Acors
Bexpress
C3000
D8080
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of 'cors' for middleware.
Using wrong port number.
5fill in blank
hard

Fill all three blanks to create a CORS options object allowing methods GET and POST, origin 'https://site.com', and enable credentials.

Node.js
const corsOptions = {
  origin: [1],
  methods: [2],
  credentials: [3]
};
Drag options to blanks, or click blank then click option'
A'https://site.com'
B['GET', 'POST']
Ctrue
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an array for methods.
Setting credentials to false when true is needed.