diff --git a/src/app.service.ts b/src/app.service.ts index 2c250f3..3a19ca9 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -1,3 +1,20 @@ -export const getLocationOfTarget = (target: string) => { - return `Location of ${target}`; +import axios from 'axios'; + +const fetchTarget = async (target: string) => { + return await axios.get(target, { + maxRedirects: 0, + validateStatus: (status) => { + return status >= 300 && status < 400; + }, + }); +}; + +export const getLocationOfTarget = async (target: string) => { + try { + const { headers } = await fetchTarget(target); + return headers.location as string; + } + catch (err) { + return undefined; + } }; diff --git a/src/app.ts b/src/app.ts index 3895f1b..d0a5fcf 100644 --- a/src/app.ts +++ b/src/app.ts @@ -3,11 +3,16 @@ import { getLocationOfTarget } from './app.service'; let app = express(); -app.get('/*', (req, res) => { +app.get('/*', async (req, res) => { const target = req.url.slice(1); - const location = getLocationOfTarget(target); - res.send(location); + const location = await getLocationOfTarget(target); + if (location) { + res.redirect(location); + } + else { + res.sendStatus(404); + } }); app.listen(3000, () => {