import axois from "axios";
app.get("/", async (req, res) => {
try{
const response = await axios.get("https://bored-api.appbrewery.com/random");
res.render("index.ejs", {activity: response.data});
} catch (error) {
console.error("Failed to make requests:", error.message);
res.status(500).send("Failed to fetch activity. Please try again.");
}
});
import express from "express";
import bodyParser from "body-parser";
import axios from "axios";
const app = express();
const port = 3000;
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
// Step 1: Make sure that when a user visits the home page,
// it shows a random activity.You will need to check the format of the
// JSON data from response.data and edit the index.ejs file accordingly.
app.get("/", async (req, res) => {
try {
const response = await axios.get("https://bored-api.appbrewery.com/random");
const result = response.data;
res.render("index.ejs", { data: result });
} catch (error) {
console.error("Failed to make request:", error.message);
res.render("index.ejs", {
error: error.message,
});
}
});
app.post("/", async (req, res) => {
console.log(req.body);
try {
const type = req.body.type;
const participants = req.body.participants;
const response = await axios.get(`https://bored-api.appbrewery.com/filter?type=${type}&participants=${participants}`);
const result = response.data;
console.log(result);
res.render("index.ejs", {
data: result[Math.floor(Math.random() * result.length)],
});
} catch (error) {
console.error("Failed to make request:", error.message);
res.render("index.ejs", {
error: "No activities that match your criteria.",
});
}
// Step 2: Play around with the drop downs and see what gets logged.
// Use axios to make an API request to the /filter endpoint. Making
// sure you're passing both the type and participants queries.
// Render the index.ejs file with a single *random* activity that comes back
// from the API request.
// Step 3: If you get a 404 error (resource not found) from the API request.
// Pass an error to the index.ejs to tell the user:
// "No activities that match your criteria."
});
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});
import axios from "axios";
app.get("/", async (req, res) => {
try {
const response = await axios.get("URL", {
params: {
ID: 12345,
},
});
res.json({data: response.data});
} catch(error) {
res.status(404).send(error.response.data);
}
});
import axios from "axios";
axios
.get("URL", {
params: {
ID: 12345,
},
})
.then(function (response) {
res.json({ data: response.data});
})
.catch( function (error) {
res.status(404).send(error.response.data);
})
// HINTS:
// 1. Import express and axios
import express from "express";
import axios from "axios";
// 2. Create an express app and set the port number.
const app = express();
const port = 3000;
// 3. Use the public folder for static files.
app.use(express.static("public"));
/*
So we do app.use(),
and then in the Express package there is something called static, which is a built-in middleware function
and allows us to serve static files and allows us to specify which folder contains the static files,
which in our case, as you can see, is called public.
Now, what this means is that any static files that are being used will be able to refer to public and
then can add the relative path after that location.
*/
// app.use(bodyParser.urlencoded({ extended: true }));
// 4. When the user goes to the home page it should render the index.ejs file.
// app.get("/", (req,res) => {
// render("./views/index.ejs");
// })
// 5. Use axios to get a random secret and pass it to index.ejs to display the
// secret and the username of the secret.
app.get("/", async (req, res) => {
try {
const response = await axios.get("https://secrets-api.appbrewery.com/random");
const result = response.data;
console.log(result);
res.render("index.ejs", {
secret: result.secret,
user: result.username,
});
} catch(error) {
console.error("Failed to make request:", error.message);
res.render("index.ejs", {
error: error.message,
});
}
});
// 6. Listen on your predefined port and start the server.
app.listen(port, ()=>{
console.log(`Server running on port: ${port}`);
})
'프로그래밍 > Web' 카테고리의 다른 글
make my own API (0) | 2024.03.14 |
---|---|
DIY API (0) | 2024.03.14 |
What is Javascript? (0) | 2023.05.25 |
temp title (0) | 2023.05.24 |
CSS Positioning (0) | 2023.05.23 |