NEW
Font size
WorksheetsMERN Stack (Backend + Frontend Connection)
Total questions: 17
Worksheet time: 9mins
What does the mongod command do?
A. Starts the MongoDB client shell to interact with databases
B. Installs MongoDB on the system
C. Starts the MongoDB server
D. Creates a new MongoDB database with default settings
What does mongosh do?
A. Starts the MongoDB server (the program that stores your data)
B. Opens the MongoDB shell (a place where you can type commands to control the database)
C. Installs MongoDB on your computer
D. Deletes a database automatically
What does node server.js do?
A. Starts the Node.js program and runs the code inside server.js
B. Installs the Node.js server on your computer
C. Opens a shell to write Node.js commands manually
D. Deletes the server.js file and its folder
What does nodemon server.js do?
A. Runs server.js one time using Node.js
B. Automatically restarts server.js whenever you make changes to the file
C. Deletes and recreates server.js repeatedly
D. Opens a shell to manually run server.js commands
import React, { useState } from 'react';
import './App.css';
A. It loads React and a CSS file to style the app, and lets you use the useState feature to manage data inside the app.
B. It runs the app and applies all styles automatically.
C. It connects the app to a database and imports the needed CSS from React itself.
D. It exports the React app to the browser and saves the CSS file inside it.
function App() {
const [formData, setFormData] = useState({ email: '', password: '' });
A. It creates a state variable called formData with default empty values for email and password, and allows updating them using setFormData.
B. It sends the email and password to the server automatically.
C. It permanently stores email and password in the browser's storage.
D. It deletes the email and password every time the page reloads.
const handleChange = (e) => {
setFormData(prev => ({
...prev,
[e.target.name]: e.target.value
}));
};
A. It updates the specific field in formData when a form input changes, keeping the other fields unchanged.
B. It resets all form fields to empty whenever any input is changed.
C. It sends the entire form data to the server immediately after any input change.
D. It deletes the form data whenever the input value is cleared.
const handleSubmit = async (e) => {
e.preventDefault();
A. It clears all inputs in the form.
B. It stops the form from refreshing the page when submitted.
C. It restarts the app whenever the form is submitted.
D. It sends the form data to the server immediately.
try {
const res = await fetch('http://localhost:5000/api/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(formData)
});
A. It deletes data from the server at the given URL.
B. It saves the formData to the user’s local storage automatically.
C. It sends the formData as a JSON POST request to the specified server URL and waits for a response.
D. It retrieves data from the server using a GET request.
const data = await res.json();
alert(data.message);
} catch (err) {
console.error(err);
alert('Error submitting form');
}
};
A. It converts the server’s JSON response into a JavaScript object and shows a message from the response.
B. It sends another request to the server automatically after the first one.
C. It clears the form inputs if the response is successful.
D. It restarts the app if there is an error during the request.
return (
<div className="App">
<header className="App-header">
<h2>Login Form</h2>
<form onSubmit={handleSubmit} style={{ display: 'flex', flexDirection: 'column', gap: '10px', width: '300px' }}>
<input type="email" name="email" placeholder="Email" onChange={handleChange} value={formData.email} required />
<input type="password" name="password" placeholder="Password" onChange={handleChange} value={formData.password} required />
<button type="submit">Login</button>
</form>
</header>
</div>
);
A. It displays a styled login form with email and password fields, and handles input changes and form submission.
B. It sends the login data to the server automatically without a form.
C. It shows a registration form with username and password inputs.
D. It deletes all input fields and disables the login button.
export default App;
A. Runs the App component automatically when the file loads.
B. Deletes the App component from the project.
C. Imports the App component from another file.
D. Makes the App component the default export, so other files can import it easily.
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
const PORT = 5000;
A. Starts the server immediately on port 5000.
B. Connects to a MongoDB database called cors.
C. Imports the app module from Express and sets the port to 3000.
D. Imports necessary packages (express, mongoose, cors), creates an Express app instance, and sets the port to 5000.
// Middleware
app.use(cors());
app.use(express.json());
A. They enable CORS (allow cross-origin requests) and parse incoming JSON request bodies.
B. They start the server and listen on a port.
C. They create a new database and a collection.
D. They send JSON responses to clients automatically.
// MongoDB Connection
mongoose.connect('mongodb://localhost:27017/mern_login', {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => console.log('MongoDB connected'))
.catch(err => console.log(err));
A. It starts a MongoDB server on your local machine automatically.
B. It connects to a MongoDB database named mern_login on localhost and logs success or error messages.
C. It deletes the mern_login database if it exists.
D. It creates a new MongoDB user with the username mern_login.
// Mongoose Schema & Model
const UserSchema = new mongoose.Schema({
email: String,
password: String
});
const User = mongoose.model('User', UserSchema);
A. It creates a schema defining a User with email and password fields and makes a model to interact with the User collection in MongoDB.
B. It deletes the User collection from the database.
C. It starts the MongoDB server and connects to the User database.
D. It imports user data from a JSON file and saves it to the database.
app.post('/api/login', async (req, res) => {
const { email, password } = req.body;
try {
const newUser = new User({ email, password });
await newUser.save();
res.json({ message: 'User saved successfully' });
} catch (err) {
res.status(500).json({ message: 'Error saving user' });
}
});
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));
A. It deletes a user from the database when a POST request is made to /api/login.
B. It listens for login requests but does not handle saving users.
C. It starts the server but doesn’t listen for any routes.
D. It handles POST requests to /api/login by creating and saving a new user, then starts the server listening on a port.
