I need to know how to send the GET request to retrieve the URL from the database after the POST request to send it there. Also, should I do app.get in the callback for app.post?
Not quite. dns.lookup accepts a domain as its first argument. However, you are passing the full URL to it.
Again, not quite. Remember, all you are doing when you write app.get/app.post is defining a route. It does not make sense to define one inside the other. In fact, I would imagine this would cause some very unexpected behaviour.
This is what the get request looks like: /api/shorturl/<short_url>
So, the URL wanted is in the short_url parameter.
This can be done in many ways. To keep mine simple, I just used the number of documents in my database. That is, every document in the db had a number related to the its position in the db.
I think you might be making this much more complicated than need be. I suggest you sit back from your keyboard for a few minutes just paying attention to the user stories, and simplifying the workflow.
So I need a URL parameter? The POST request is to /api/shorturl/new and the GET request is to /api/shorturl/<short_url>? Or are they both supposed to be to the latter?
I’m thinking I’ll just use the first 2 or so digits of the _id. Or would that be a bad idea?
Then I should probably assign a number to each document based on order of creation. I was already thinking of that from the beginning but I don’t know how to do it.
For the GET request, I just have to get the original URL from the database and make a redirect to it, right?
how do I get the original URL from here on the GET request? I’m not clear on that. And aside from the short_url key’s value, which I don’t have yet, how is my POST code so far?
Also, where in the POST request part should I send the response with the original and short URLs in it? At the bottom of the dns.lookup callback?
If you pass an invalid URL that doesn’t follow the valid http://www.example.com format, the JSON response will contain { error: 'invalid url' }
How do you know what err will hold?
You have the original URL in your database…you need to find it, when someone says “take me to short_url 4”, then you find what “short_url 4” represents in your database, and redirect.
Anywhere… Provided there are no errors in the request (and your logic), and you have the data to send, you can just res.json it. Logically, you have steps to follow:
Accept URL in POST request
Ensure URL is of the correct format (what dns.lookup is doing)
Save URL to database with associated shortened version
What I did was something like this (not actual code, but you get the just):
const short_url = Model.find({}).length; // number of documents in db
const newDoc = new Model({short_url, original_url}); // create new doc
newDoc.save(cb); // Save to db
Then, to get the original URL:
const givenShortURL = req.params.short_url; // From GET req
Model.findOne({short_url: givenShortURL}, cb); // Find and redirect in cb
I still don’t seem to be getting the URL into the database. What’s logged by me doing console.log(url ${url} saved to database); is url { _id: 60454ad3bcdcdb05cc267529, __v: 0 } saved to database which shows neither the short URL nor the long one.
Any help is appreciated. Thanks.
Edit: The response I get in my browser for the GET request doesn’t have the short URL in it either. Just the original one.
Model.find is asynchronous. (same goes for all db operations)
You need to be working with promises, when you do these operations.
What is the point of this:
You are getting there, but really take the time to work through this. Take a break, if you need one. Otherwise, you could end up introducing more bugs than you resolve.