42
loading...
This website collects cookies to deliver better user experience
npm i @tinyhttp/app @tinyhttp/logger milliparsec memjs
import { App } from "@tinyhttp/app";
import { logger } from "@tinyhttp/logger";
import { json } from "milliparsec";
const app = new App();
app.use(logger());
app.use(json());
// More stuff comes here.
app.listen(3333);
id
:app.post("/:id", (req, res) => {
// Logic goes here.
});
app.post("/:id", (req, res) => {
const { id } = req.params;
const data = { id, ...req.body };
// More logic goes here.
});
status code 201
(to indicate that the data was added to Memcached) and the respective object that was created by us.app.post("/:id", (req, res) => {
const { id } = req.params;
const data = { id, ...req.body };
return res.status(201).json(data);
});
import { App } from "@tinyhttp/app";
import { logger } from "@tinyhttp/logger";
import { json } from "milliparsec";
import { Client } from "memjs";
const app = new App();
const memcached = Client.create();
// Hidden for simplicity
.set()
method to input some data. key
, which in this case is the id. value
of that same key, which we must convert to a string. .set()
method returns a Promise.app.post("/:id", async (req, res) => {
const { id } = req.params;
const data = { id, ...req.body };
await memcached.set(id, JSON.stringify(data), { expires: 12 });
return res.status(201).json(data);
});
const verifyCache = (req, res, next) => {
// Logic goes here.
};
const verifyCache = (req, res, next) => {
const { id } = req.params;
// More logic goes here.
};
.get()
method. Let's pass two arguments in this method, the first argument will be the id
. The second argument will be a callback
and will also have two arguments. The first will be the error
, the second will be the key value
.const verifyCache = (req, res, next) => {
const { id } = req.params;
memcached.get(id, (err, val) => {
// Even more logic goes here.
});
};
const verifyCache = (req, res, next) => {
const { id } = req.params;
memcached.get(id, (err, val) => {
if (err) throw err;
// Even more logic goes here.
});
};
status code 200
(to show that it was obtained from Memcached successfully) and we will send our json object (but first it must be converted from string to json).const verifyCache = (req, res, next) => {
const { id } = req.params;
memcached.get(id, (err, val) => {
if (err) throw err;
if (val !== null) {
return res.status(200).json(JSON.parse(val));
} else {
return next();
}
});
};
app.post("/:id", verifyCache, async (req, res) => {
const { id } = req.params;
const data = { id, ...req.body };
await memcached.set(id, JSON.stringify(data), { expires: 12 });
return res.status(201).json(data);
});
import { App } from "@tinyhttp/app";
import { logger } from "@tinyhttp/logger";
import { json } from "milliparsec";
import { Client } from "memjs";
const app = new App();
const memcached = Client.create();
app.use(logger());
app.use(json());
const verifyCache = (req, res, next) => {
const { id } = req.params;
memcached.get(id, (err, val) => {
if (err) throw err;
if (val !== null) {
return res.status(200).json(JSON.parse(val));
} else {
return next();
}
});
};
app.post("/:id", verifyCache, async (req, res) => {
const { id } = req.params;
const data = { id, ...req.body };
await memcached.set(id, JSON.stringify(data), { expires: 12 });
return res.status(201).json(data);
});
app.listen(3333);