27
loading...
This website collects cookies to deliver better user experience
npm install express node-cache axios
const express = require("express");
const app = express();
app.get("/", (req, res) => {
return res.json({ message: "Hello world 🇵🇹" });
});
const start = (port) => {
try {
app.listen(port);
} catch (err) {
console.error(err);
process.exit();
}
};
start(3333);
app.get("/todos/:id", async (req, res) => {
try {
// Logic goes here
} catch () {
// Some logic goes here
}
});
id
parameter to get its to-do. Then we will make the http request using axios
. Finally, let's return the data from the response.const axios = require("axios");
// Hidden for simplicity
app.get("/todos/:id", async (req, res) => {
try {
const { id } = req.params;
const { data } = await axios.get(`https://jsonplaceholder.typicode.com/todos/${id}`);
return res.status(200).json(data);
} catch () {
// Some logic goes here
}
});
.sendStatus()
method.app.get("/todos/:id", async (req, res) => {
try {
const { id } = req.params;
const { data } = await axios.get(`https://jsonplaceholder.typicode.com/todos/${id}`);
return res.status(200).json(data);
} catch ({ response }) {
return res.sendStatus(response.status);
}
});
const express = require("express");
const NodeCache = require("node-cache");
const axios = require("axios");
const app = express();
const cache = new NodeCache({ stdTTL: 15 });
// Hidden for simplicity
const verifyCache = (req, res, next) => {
try {
// Logic goes here
} catch () {
// Some logic goes here
}
};
id
from the parameters, then we will check if there is any property with the same id in the cache. If there is, we will get its value, however, if it does not exist, it will proceed to the controller. If an error occurs, it will be returned.const verifyCache = (req, res, next) => {
try {
const { id } = req.params;
if (cache.has(id)) {
return res.status(200).json(cache.get(id));
}
return next();
} catch (err) {
throw new Error(err);
}
};
app.get("/todos/:id", verifyCache, async (req, res) => {
try {
const { id } = req.params;
const { data } = await axios.get(`https://jsonplaceholder.typicode.com/todos/${id}`);
cache.set(id, data); // also added this line
return res.status(200).json(data);
} catch ({ response }) {
return res.sendStatus(response.status);
}
});
const express = require("express");
const NodeCache = require("node-cache");
const axios = require("axios");
const app = express();
const cache = new NodeCache({ stdTTL: 15 });
const verifyCache = (req, res, next) => {
try {
const { id } = req.params;
if (cache.has(id)) {
return res.status(200).json(cache.get(id));
}
return next();
} catch (err) {
throw new Error(err);
}
};
app.get("/", (req, res) => {
return res.json({ message: "Hello world 🇵🇹" });
});
app.get("/todos/:id", verifyCache, async (req, res) => {
try {
const { id } = req.params;
const { data } = await axios.get(`https://jsonplaceholder.typicode.com/todos/${id}`);
cache.set(id, data);
return res.status(200).json(data);
} catch ({ response }) {
return res.sendStatus(response.status);
}
});
const start = (port) => {
try {
app.listen(port);
} catch (err) {
console.error(err);
process.exit();
}
};
start(3333);