Tell us what’s happening:
I have been struggling with this URL Shortener project for a while.
Here is my replit link
I am trying to get this Replit code running on the console, but it will not run the preview of the URL Shortener.
I used my same MONGO_URI code from the first cluster I created in MongoDB Atlas.
I put the MONGO_URI code into the Secrets (Environment Variable) file. Did I put it in the server.js file correctly? If not, how do I put in my mongodb in this project? I also installed the latest versions of mongoose & mongodb by typing in npm install mongoose
and npm install mongodb
in Replit Console. These were automatically updated in the package.json file within the dependencies array.
Code I am working on the server.js in Replit starter project
const express = require('express');
const mongo = require('mongodb');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const dns = require('dns');
const app = express();
const mongo_key = process.env.MONGO_URI;
const Schema = mongoose.Schema;
// BASIC CONFIGURATION
const port = process.env.PORT || 3000;
// I added my MONGO_URI code in this project. Is this the proper way to add mongodb?
mongoose.connect(process.env.MONGO_URI);
// const mySecret = process.env['MONGO_URI']
// mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true} );
const urlSchema = new Schema({
id: Number,
url: String,
});
const urlModel = mongoose.modl('Url', urlSchema);
app.use(cors());
app.use(bodyParser());
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/new", function (req, res) {
let original = req.body;
let theData;
let urlRegex = /https:\/\/www.|http:\/\/www./g;
dns.lookup(req.body.url.replace(urlRegex, ''), (err, address, family) => {
if(err) {
res.json({"error": err});
} else {
onComplete();
}
});
function onComplete(){
urlModel.find()
.exec()
.then(docs => {
theData = docs;
var doc = new urlModel({"id": theData.length, "url": req.body.url });
theData = theData.filter((obj) => obj["url"] === req.body.url);
if ( theData.length === 0 ) {
doc.save()
.then(result => {
res.json(result);
})
.catch(err = {
res.json({"error": err});
});
} else {
res.json({"error": `URL already in database as ${theData[0].id}` });
}
})
.catch(err => {
res.json({"error": err});
});
};
});
app.get("/api/shorturl", function (req, res) {
urlModel.find()
.exec()
.then(docs => {
res.json(docs);
})
.catcj(err => {
res.json({"error": err});
});
});
app.get("/api/shorturl/:short", function (req, res) {
console.log(req.params.short);
let short = req.params.short;
urlModel.find({"id": short}).exec()
.then(docs => {
res.redirect(docs[0]["url"]);
})
.catch(err => {
res.json({"error": err});
})
});
app.listen(port, function() {
console.log(`Listening on port ${port}`);
});
- Are there any syntax errors I made?
- Am I missing any specific codes?
- Do I have to arrange the code in a certain order?
- Did I do something wrong with the mongodb code, like did I put it in wrong?
- Do I only have to work on the server.js in Replit? Are there other parts of Replit I need to work on?
I appreciate any help for this. Thank you
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
Challenge: URL Shortener Microservice
Link to the challenge: