I’ve tested this and all works fine, but am failing the 3rd test still:
When you visit
/api/shorturl/<short_url>
, you will be redirected to the original URL.
I’ve looked at other threads and there are moderators showing github links to to actual tests, but these are now all expired pages. How are we supposed to know what the actual test are am I missing something, there is nothing in the readme?
Can any one help please.? I have copied my code below because when I publish the replit and copy link here it never seems to work:
const express = require('express');
const cors = require('cors');
const app = express();
const bodyParser = require('body-parser');
const lookup = require('dns-lookup');
// Basic Configuration
const port = process.env.PORT || 3000;
app.use(cors());
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
//app.use(bodyParser.json())
app.use('/public', express.static(`${process.cwd()}/public`));
app.get('/', function(req, res) {
res.sendFile(process.cwd() + '/views/index.html');
});
// Your first API endpoint
app.get('/api/hello', function(req, res) {
res.json({ greeting: 'hello API' });
});
let arr = [];
let id = 0;
app.post('/api/shorturl', function(req, res, next){
//regex to remove everthing apart from domain
//const regex = /^[^.]*\./;
const regexHTTP = /^http|https|www./;
let url = req.body.url;
//let domain = url.replace(regex, "");
//check if valid url
//removed the dns lookup as fcc tests didn't like it. Basic regex for https, http or www. passed the second test
/*
lookup(domain, function (err, address, family) {
console.log("address is:", address);
console.log("family is:", family);
if(err){
res.json({ error: 'invalid url' })
}else{
arr.push({
original_url: url,
short_url: id
})
res.json(arr[id]);
id++;
}
});
*/
if(!url.match(regexHTTP)){
res.json({ error: 'invalid url' })
}else{
arr.push({
original_url: url,
short_url: id
})
res.json(arr[id]);
id++;
}
next();
})
app.get('/api/shorturl/:number', function(req, res) {
//change input string to number
let number = parseInt(req.params.number);
//locate original_url from index of arr
let foundUrl = arr[number].original_url;
console.log("foundUrl is:", foundUrl);
/*
if(foundUrl === "undefined"){
res.json({ error: 'invalid url' })
}
*/
//regex to check for pre domain chars
const regexHTTP1 = /^http|https|www./;
//strip url to domain only
let strippedUrl = foundUrl.replace(regexHTTP1, "");
//create new url
let newUrl = `https://${strippedUrl}`;
console.log("new url is:", newUrl)
return res.redirect(newUrl);
});
app.listen(port, function() {
console.log(`Listening on port ${port}`);
});```
**Your browser information:**
User Agent is: <code>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36</code>
**Challenge:** Back End Development and APIs Projects - URL Shortener Microservice
**Link to the challenge:**
https://www.freecodecamp.org/learn/back-end-development-and-apis/back-end-development-and-apis-projects/url-shortener-microservice