Back End Development and APIs Projects - URL Shortener Microservice

I’m a beginner in nodejs and I’m having trouble passing the tests about the tests about redirecting to the original url and returning the json response even though I have tested multiple times that these functions work. The link to my project is boilerplate-project-urlshortener - Replit and my code so far is as follows:

const express = require('express');
const cors = require('cors');
const app = express();
const mongoose = require("mongoose")
const Url = require('./url')
const baseURL = "http://localhost:3000"
const dns = require('node:dns');

const MongoClient = require('mongodb').MongoClient;
const mongo_username = process.env['MONGO_USERNAME']
const mongo_password = process.env['MONGO_PASSWORD']

// const uri = `mongodb://<${mongo_username}>:<${mongo_password}>,,`;

const uri = `mongodb://${mongo_username}:${mongo_password},,`;

const client = new MongoClient(uri, { useNewUrlParser: true });

mongoose.set('strictQuery', false)
// Basic Configuration
const port = process.env.PORT || 3000;


app.use(express.urlencoded({ extended: true }))

const shortid = require('shortid');

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' });

app.get('/api/shorturl/:id', async function(req, res) {
  client.connect(async (err) => {
    if (err) console.log(err)
    const url = client.db("urlShortener").collection('Urls')
    const existingUrl = await url.findOne({ shortURL: }) || null
    if (existingUrl) {
      return res.redirect(existingUrl.longUrl)
    else res.json({ message: "non existing url" })
})'/api/shorturl', async (req, res) => {
  const isValidUrl = (urlString) => {
    let url;
    try {
      url = new URL(urlString);
    catch (e) {
      return false;
    return url.protocol === "http:"
  const urlInput = req.body.url
  if (!isValidUrl(urlInput)) return res.json({ error: 'invalid url' })
  else {
    client.connect(async (err) => {
      if (err) console.log(err)
      const url = client.db("urlShortener").collection('Urls')
      const existing = await url.findOne({ longUrl: urlInput })
      if (existing) return res.json({
        original_url: existing.longUrl,
        short_url: existing.shortURL
      const id = shortid.generate()
      const newUrl = await url.insertOne({
        longUrl: urlInput,
        shortURL: id
      return res.json({ original_url: urlInput, short_url: id })



app.listen(port, function() {
  console.log(`Listening on port ${port}`);

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:

When I submit (a valid url)`, your app responds with:

{"error":"invalid url"}
1 Like

Oh wow I thought I should just follow what’s in the tests that only urls with protocols of http should be considered as valid it turns out that https is considered as valid too I just went and included it and it just passed all the tests. Thank you!

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