Back End Development and APIs Projects - URL Shortener Microservice

Tell us what’s happening:
I’m done building the url shortener application and when testing both locally and on live server everything works as should but, when I run the test on FCC it fails can someone please assist?

Your project link(s)

solution: URL Shortener

githubLink: GitHub - KingGoku910/kinggoku90-backend-portfolio-app: FCC Backend Microservice Projects for my Web App Portfolio

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Back End Development and APIs Projects - URL Shortener Microservice

Link to the challenge:

Here is my code for the URL Shortner :

const express = require(‘express’);
var mongo = require(‘mongodb’);
var mongoose = require(‘mongoose’);
var bodyParser = require(‘body-parser’);
var shortId = require(‘shortid’);
const app = express();
const port = process.env.PORT || 3000;
var dns = require(‘dns’);

//Setup DB

mongoose.connect(process.env.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true

//URL Shortner
// Build a schema and model to store saved URLS
var ShortURL = mongoose.model(‘ShortURL’, new mongoose.Schema({
short_url: String,
original_url: String,
suffix: String
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())“/api/shorturl/new/”, (req, res) => {
let client_requested_url = req.body.url

let suffix = shortid.generate();
let newShortURL = suffix

let newURL = new ShortURL({
    short_url: __dirname + "/api/shorturl/" + suffix,
    original_url: client_requested_url,
    suffix: suffix
}), doc) => {
    if (err) return console.error(err);
        "saved": true,
        "short_url": newURL.short_url,
        "orignal_url": newURL.original_url,
        "suffix": newURL.suffix


app.get(“/api/shorturl/:suffix”, (req, res) => {
let userGeneratedSuffix = req.params.suffix;
ShortURL.find({ suffix: userGeneratedSuffix }).then(foundUrls => {
let urlForRedirect = foundUrls[0];

app.get(“/api/shorturl/:suffix”, (req, res) => {
let userSuffix = req.params.suffix;
ShortURL.find({ suffix: userSuffix }).then((foundUrls) => {
let urlForRedirect = foundUrls[0];



Looking at the code, it’s missing URL validation so I would guess that is a problem. You also have duplicate redirect routes for stored URLs. Also, mongoDB assigns a unique ID to each document stored, so generating an extra one with shortID is just extra work.

Post a link to a running REPL for better debugging.

I think something is wrong with the test I have recoded the entire shortner section exactly to the specs, including error handling.

Here’s the URL to my Live site: I prefer using my own code editor thus I don’t make use of repl.


Here’s the new code and tests work fine on my live heroku app, functionally exact to the example provided. And still all tests are failing except for the first one? See code below:

app.use(bodyParser.urlencoded({ extended: true }));

//URL Shortner
const originalUrls = [];
const shortUrls = [];"/api/shoicerturl/new", (req, res) => {
    const url = req.body.url
    const foundIndex = originalUrls.indexOf(url);

    if (!url.includes("https://") && !url.includes("http://")) {
    return res.json({ error: "Invalid url" })

    if (foundIndex < 0) {

        return res.json({
            original_url: url,
            short_url: shortUrls.length -0

    return res.json({
        original_url: url,
        short_url: shortUrls[foundIndex]

app.get("/api/shorturl/:shorturl", (req, res) => {
    const shorturl = parseInt(req.params.shorturl)
    const foundIndex = shortUrls.indexOf(shorturl);

    if (foundIndex < 0) {
        return res.json({
            "error": "No short URL found for the given input"

No, the tests work correctly.

A live link to an app does not help others debug the code. Visual code inspection is rarely helpful. A live app only lets the tests be run against it, without output. Others need access to the code so they can easily fork and run it and add debugging output to actually diagnose what the code is doing. A live app doesn’t do that and the easier it is to help, the more help you’ll get. Code in git still has to be hosted, which means others would have to pull your code and run it somewhere to debug it. So, share your project on heroku (the code, not the link to the live site) or move to and post the link to the project there so people can run and debug.

Or, add debugging to your code and run the tests against the app. Log the route name, route inputs, and route responses (everywhere there is res.whatever()) for all routes and you’ll see what the code is doing on the server console.

Okay here is the github repo: GitHub - KingGoku910/kinggoku90-backend-portfolio-app: FCC Backend Microservice Projects for my Web App Portfolio

I see a bunch of weird routes that don’t match up to what the tests are looking for, like"/api/shorturl/new", but without a running app I can’t test to see what output everything is giving. I’d guess that’s a good place to start looking though.

I passed this test myself two days ago, so it was working fine then. Good luck!

should /new not be included?

The test says that:

You can POST a URL to /api/shorturl and get a JSON response with original_url and short_url properties. Here's an example: { original_url : '', short_url : 1}

All of these tests have VERY specific routes, since they’re meant to be APIs. I’d take a closer look at all of them to make sure their computer is finding what it is expecting!

Also, it can help to put a ton of console.log()s in so you can see what their server is sending you and what you are sending back. I got stuck on this same one for a while until I read my logs and saw why I was failing one of the tests.

I figured it out just a few monments ago I passed the test and the live app is also working as expected now. But I’ll give you the solve anyway as your directive was correct.

Sorry just realised someone also pointed it out before you, so sorry he’ll have to get the credit for this post. I appologise.

I have the same thing in your github but it doesn't work for me sorry explain to me how you did 

type or paste code here

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.