Microservice url shortner - code error

can someone have a look at the code:

const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const shortID = require('shortid');
const app = express();

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

const urlSchema = new mongoose.Schema({
  originalUrl: String,
  shortUrl: String
const URL = mongoose.model("URL", urlSchema);

// Basic Configuration
const port = process.env.PORT || 3000;

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

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.post('/api/shorturl', async (req, res) => {
const url = req.body.url;

if (!url.includes('https://') && !url.includes('http://')) {
  return res.json({
    error: 'invalid url'

  let findOne = await URL.findOne({ originalUrl: url });

  if (findOne) {
      original_url: findOne.originalUrl,
      short_url: findOne.shortUrl
  } else {
    findOne = new URL({
      originalUrl: url,
      short_url: shortID.generate()

    await findOne.save();
      original_url: findOne.originalUrl,
      short_url: findOne.shortUrl

app.get('api/shorturl/:shorturl', async (req,res) => {
  const shorturl = req.params.shorturl;

  let findOne = URL.findOne(shorturl);

  if (!findOne) {
    return res.json({
      "error": "No short URL found for the given input"

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

somekind of errors

(node:78299) UnhandledPromiseRejectionWarning: MongoServerError: bad auth : Authentication failed.

is there any major fault in the code or some improvements?

Looks like you’re not authenticating. Your mongoDB username/password should be in your URI. There’s probably also a line number in the error referencing this line

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });

If so, then there is some problem with the URI or your credentials.

Posting a link to a repl is likely to get more accurate help.

here is the link:

That’s the live site, not the repl (code).

FYI - You can get to the code url by breaking the live project url into pieces:

boilerplate-project-urlshortener is the project name.
zia73 is the username

Then you can build the code url by:

In this case, that would be:

That has quit working of late for me (it was working up to about a week ago). No idea why, but I presume user error until I figure it out.

How did you add the connection string?

What does your connection string look like? Post it but replace the password.

One thing to look out for is to make sure your password does not include the brackets <> that are in the placeholder.

1 Like

Thank you very much Randell for the help.
I did not know how to do this .

Hi, did you mean


as from the response of Randell?

It must be the value you are using for MONGO_URI. I used mine in your project and it worked.


thanks for sharing this.

That is what it would look like in an env file.

On Replit look at the raw JSON (padlock tab > “Edit raw JSON (advanced)” button) it should look like this.

  "MONGO_URI": "mongodb+srv://Zia:PASSWORD@cluster0.qamx0qz.mongodb.net/?retryWrites=true&w=majority"

If that looks correct I would suggest you double check the username and password are correct.

You might also try adding a database name to the string although I don’t think the auth error is related to that.


thanks, it looks like as you mentioned in the raw json.
i tried to re-copy the connection string and password.
now I tried to add the database and now the response is:

  const serverSelectionError = new ServerSelectionError();

MongooseServerSelectionError: Could not connect to any servers in your MongoDB At

Do you have another connection string you can try, maybe the one you used for the initial setup challenge?

Install and Set Up Mongoose

Maybe just try starting over. Create a new cluster and user and see if that works.

Here is the setup article as well just so you can double-check things.

1 Like

thanks I will check this, I was trying locally and it works fine, but shorturl is not being created

findOne = new URL({
      originalUrl: url,
      short_url: shortID.generate()

in the database it _id and originalUrl but no short id?

it seems, its not generating short url id?