I use mongoose this is my schema:
const ShortUrlModel = mongoose.model(
'ShortUrl',
new mongoose.Schema({
original_url: String,
hash: String,
expires_at: { type: Date, expires: 5000 },
}),
);
I put expires in 5 secound for test, but it does not work
i wanna expire in 10 days.
when posting to my server to add a url
fastify.post('/new', async (request, reply) => {
let original_url = request.body.url;
let hash = request.body.hash;
const today = new Date();
const expires_at = new Date();
expires_at.setDate(today.getDate() + 10);
let short = await ShortUrlModel.findOne({ hash });
if (short) {
...
} else {
const newShortener = new ShortUrlModel({
original_url,
hash,
expires_at,
});
let saveUrl = await newShortener.save();
console.log(saveUrl);
reply.send({
...
});
}
});