Still stuck - URL Shortener Microservice Project

I had to repost this because the only response I received on my previous post was that I needed to add the code to it, so I did and got even less response (none).

I am working on this project, finally have an app together that does what the requirements of the project require, but it will not pass any tests but returns verbatim what the project is asking for.
What do I do from here?
** update**
Did not post code because I broke it trying to figure out why it wouldn’t accept on site. Fixed broken code, however still trying to remember how I removed the _id from the returned data. That only took half a day of research and still had to figure out a way to do it on my own, then in trying to fix the other problems (i.e. deployment errors) and lost that fix too.
Github repository for project


const cors = require( "cors" );
const mongoose = require( "mongoose" );
const app = express();
const bodyParser = require( "body-parser" );
const shortUrl = require( "./models/shortUrl.js" );
const outputUrl = require("./models/outputUrl.js");
mongoose.Promise = global.Promise;
const { workers } = require( "cluster" );
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({ extended: true}));
app.use( cors() );

// connect to db
mongoose.connect( process.env.MONGODB_URI || 'mongodb://localhost/shorturls');
    mongoose.connection.on( "error", () => console.log( "connection error:" ) );
    mongoose.connection.once( "open", () => console.log( "We're open for buisiness!" ) );
    // allows node to find static content
    app.use( express.static( __dirname + "/public" ) );
    app.get( "/new/:urlToShorten(*)", ( req, res, next ) => {
        var { urlToShorten } = req.params;
        console.log( urlToShorten );
        var expression = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi;
        var regex = expression;
        if ( regex.test( urlToShorten ) === true ) {
            var short = Math.floor( Math.random() * 100000 ).toString();
            var data = new shortUrl(
                    original_url: urlToShorten,
                    short_url: short
   err => {
                if ( err ) {
                    return res.send("Error saving to database");
          } );
          return res.send(data);
        var data = new shortUrl( {
            original_url: "urlToShorten does not match standard format",
            short_url: "Invalid URL"
        } );
        var data = new outputUrl(
                  original_url: urlToShorten,
                  short_url: short
        return res.send( data);
    } );
    // Query database and forward to stored url
    app.get( "/:urlToForward", ( req, res, next ) => {
        // stores param value
        var short_url = req.params.urlToForward;
        shortUrl.findOne( { "short_url": short_url }, ( err, data ) => {
            if ( err ) return res.send( "Error reading database" );
            var re = new RegExp( "^(http|https)://", "i" );
            var strToCheck = data.original_url;
            if ( re.test( strToCheck ) ) {
                return res.redirect( 301, data.original_url );
            } else {
                return res.redirect( 301, "http://" + data.original_url );
        } );
    } );
app.listen( process.env.PORT, () => {
    console.log( "Everything is working" );
} );


var app = angular.module( "shortUrlApp", [] );
app.controller( "shortAppCtrl", ( $scope ) => {
} );


// template of document for shortUrl
const mongoose = require( "mongoose" );
const Schema = mongoose.Schema;
const urlSchema = new Schema( {
    original_url: String,
    short_url: String
}, {timestamps: true} );
// table name and schema
const ModelClass = mongoose.model( "shortUrl", urlSchema );
module.exports = ModelClass;


<!DOCTYPE html>
<html ng-app="shortUrlApp">
        <link rel="stylesheet" href="" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg320mUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <link rel="stylesheet" href="/app/public/style.css">
        <script src=""></script>
        <script src="js/app.js"></script>
    <body class="text-center">
    <h3 id="title">URL Shortener</h3>
    <h4>Assisted by and CodingTutorials360</h4>
    <h6>Technologies and Packages Used: Node, Nodemon, Express, MongoDB, Mongoose, Angular, JavaScript, HTML, CSS, and BootStrap</h6>
        Enter your URL here to make it tiny!
    <section ng-controller="shortAppCtrl">
      <form method="post" action="/saveQuery">
        <input placeholder="" ng-model="urlToShorten">
        <a class="btn btn-primary" ng-href="/new/{{urlToShorten}}">Get Schwifty!</a>
      <footer>Assisted by and CodingTutorials360</footer>

Also, I am including my outputUrl Schema model. The reason for the second model is because when I used Math.random (at least this is the reasoning I found) my data output automatically includes a unique id with the key _id, however nothing I tried allowed my to remove the _id from the returned json object (I was unsure whether that was why my tests were failing). I found an example that showed by adding a second json object in the model like this:

const outputSchema = new Schema(
{ original_url: urlToShorten,
  short_url: short
}, {_id: false, id: false});

This enables the schema to remove the _id from the json object. However, I had to make a second model in order to do this because the database has to store the _id value in order to link it to the original_url, so a second model was required to return the value without the _id value being shown.

My outputUrl setup broke on my when I was trying to get testing to work and I am working on fixing that now.
I am just looking for information relating to why I cannot get even the first test to pass when my output looked exactly like this:


Is it the quotes? The output on the project page is:

{ original_url : '', short_url : 1}

I thought this was the dilemma on a past project but I cannot remember if that was the issue or not…
Any input is greatly appreciated

Note that the tests expect an /api/shorturl endpoint.

Thanks Ben!!! I have 2 passing so far, making changes to get the rest up and going. The sad thing is I had already tried this before and it wasn’t working (must have been tired and put stuff in the wrong things).

1 Like

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