Answer :
You cannot use the HTTP module, only Express. If the HTTP module is used instead of Express you will receive a 0.
Write a server called zillow.js that serves JSON data when it receives a GET
request to the path ’/v1/zillow/zestimate’. Expect the request to contain aquery string with a keys ’sqft’, ’bed’, and ’bath’ all of which will be integers.The Zestimate is calculated as follows: Zestimate == sqft * bed * bath * 10and you should return to the use JSON in the following format: {zestimate:
Number }
For example: /v1/zillow/zestimate?sqft=2000&bed=3&bath=4
The JSON response should contain only the ‘zestimate’ property:
For example: {"zestimate": 240000}
Add a second endpoint at the path ‘/v1/zillow/houses’ that accepts an optional parameter ‘city‘. If city is provided as a parameter, the return all houses
that match the given city. If no city parameter is provided, then return all
houses in the database. If a city is provided and the city is not found, return
an empty array []
You will not connect to a real database, but to simulate one, use this array of
objects:
[ { price: 240000, city: "Baltimore" }, { price: 300000, city: "austin" },
{ price: 400000, city: "austin" }, { price: 1000000, city: "seattle"}, { price:
325000, city: "baltimore" }, { price: 550000, city: "seattle" }, { price: 250000,
city: "boston" } ]
For example: /v1/zillow/houses?city=baltimore
The JSON response should contain only the the list of houses in baltimore.
For example: [{ price: 240000, city: "baltimore" }, { price: 325000, city: "baltimore" }] For example: /v1/zillow/houses?city=raleigh The JSON response should contain: [] For example: /v1/zillow/houses The JSON response should contain: [ { price: 240000, city: "baltimore" }, { price: 300000, city: "austin" }, { price: 400000, city: "austin" }, { price: 1000000, city: seattle}, { price: 325000, city: "baltimore" }, { price: 550000, city: "seattle" }, { price: 250000, city: "boston" } ]
To create the server using Express and implement the specified endpoints, you can use the following code:
const express = require('express');
const app = express();
const houses = [
{ price: 240000, city: "Baltimore" },
{ price: 300000, city: "austin" },
{ price: 400000, city: "austin" },
{ price: 1000000, city: "seattle" },
{ price: 325000, city: "baltimore" },
{ price: 550000, city: "seattle" },
{ price: 250000, city: "boston" }
];
app.get('/v1/zillow/zestimate', (req, res) => {
const { sqft, bed, bath } = req.query;
const zestimate = parseInt(sqft) * parseInt(bed) * parseInt(bath) * 10;
res.json({ zestimate });
});
app.get('/v1/zillow/houses', (req, res) => {
const { city } = req.query;
if (city) {
const filteredHouses = houses.filter((house) => house.city.toLowerCase() === city.toLowerCase());
res.json(filteredHouses);
} else {
res.json(houses);
}
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
Save the above code in a file named zillow.js. Then, you can run the server using Node.js by executing the command node zillow.js. The server will start listening on port 3000.
You can test the server by making requests to the specified endpoints using tools like cURL or a web browser. For example:
To get the Zestimate: GET http://localhost:3000/v1/zillow/zestimate?sqft=2000&bed=3&bath=4
To get houses in a specific city: GET http://localhost:3000/v1/zillow/houses?city=baltimore
To get all houses: GET http://localhost:3000/v1/zillow/houses
The server will respond with the corresponding JSON data based on the requested endpoint and parameters.
To know more about HTTP module visit:
https://brainly.com/question/30187599
#SPJ11