You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
654 B
JavaScript

const express = require('express');
const index = express.Router();
const Url = require('../models/Url');
index.get('/', async (req, res) => {
res.sendFile('index.html', { root: './' })
});
index.get('/docs', async (req, res) => {
res.sendFile('docs.html', { root: './' })
});
index.get('/:urlId', async (req, res) => {
try {
const url = await Url.findOne({ urlId: req.params.urlId });
if (url) {
url.clicks++;
url.save();
return res.redirect(url.origUrl);
} else res.status(404).json('Not found');
} catch (err) {
console.log(err);
res.status(500).json('Server Error 2');
}
});
module.exports = index;