We Practice Questions
We Practice Questions
We Practice Questions
Create a simple Express.js server with a single route that responds with "Hello, World!" when
accessed via a GET request.
Define a single route (/) that responds with "Hello, World!" when accessed.
Code:
res.send('Hello, World!');
});
app.listen(port, () => {
});
Question 2
Create an Express.js route that responds with a JSON object containing a list of users when accessed
via a GET request.
Define a route (/users) that responds with a JSON object containing a list of users.
Code:
const users = [
];
res.json(users);
});
app.listen(port, () => {
});
Question 3
Create an Express.js route that allows a user to submit their name via a POST request and responds
with a greeting message that includes their name.
Define a route (/greet) that accepts a POST request with a JSON body containing a user's name.
Code:
app.use(express.json());
res.send(`Hello, ${name}!`);
});
app.listen(port, () => {
});
Question 4
Create an Express.js route that accepts a user ID as a URL parameter and responds with the user's
information if the ID exists, or a 404 error if it doesn't.
Define a route (/users/:id) that accepts a URL parameter for the user ID.
Respond with the user's information if the ID exists, otherwise respond with a 404 error.
Code:
const users = [
];
if (user) {
res.json(user);
} else {
});
app.listen(port, () => {
});
Question 5
Create an Express.js route that allows users to update their name via a PUT request. The request
should include the user ID as a URL parameter and the new name in the request body.
Define a route (/users/:id) that accepts a PUT request with a URL parameter for the user ID and a
JSON body containing the new name.
Update the user's name if the ID exists, otherwise respond with a 404 error.
Code:
app.use(express.json());
let users = [
];
users[userIndex].name = newName;
} else {
});
app.listen(port, () => {
});
data.js
// Sample data file containing users and orders
const users = [
];
const orders = [
{ id: 1, userId: 1, item: 'Laptop', quantity: 1 },
];
server.js
Here's how you can set up an Express.js server to use the data from data.js:
app.use(express.json());
res.json(users);
});
res.json(orders);
});
if (user) {
res.json(user);
} else {
});
if (order) {
res.json(order);
} else {
});
const newUser = {
id: users.length + 1,
username: req.body.username,
email: req.body.email
};
users.push(newUser);
res.status(201).json(newUser);
});
const newOrder = {
id: orders.length + 1,
userId: req.body.userId,
item: req.body.item,
quantity: req.body.quantity
};
orders.push(newOrder);
res.status(201).json(newOrder);
});
users[userIndex] = {
id: userId,
username: req.body.username,
email: req.body.email
};
res.json(users[userIndex]);
} else {
}
});
orders[orderIndex] = {
id: orderId,
userId: req.body.userId,
item: req.body.item,
quantity: req.body.quantity
};
res.json(orders[orderIndex]);
} else {
});
users.splice(userIndex, 1);
res.status(204).send();
} else {
}
});
orders.splice(orderIndex, 1);
res.status(204).send();
} else {
});
app.listen(port, () => {
});
Instructions
node server.js
The server will start and listen on port 3000. You should see a message in the terminal: Server is running
on http://localhost:3000.