Well, to start with, what would you use as the schema for mongoose?
Once you have a schema, what are your routes? You’ll probably need two: one for POST requests (to add a new route), and one for GET (to fetch a route with a given ID).
i was thinking i can take the url from the post…
do some changes in the app.post… then send it to a app.get with the shortened url as a path…
not very clear at this point but i guess using a database might be the less messier way to go then
Basically, you want to POST with a true URL, (validate that as an actual URL), and then save that to the database (or to a flat file, or to SOMETHING). Then, return the ID from that save as the shortenedURL. Handle errors etc.
Then, you want to GET with that shortened URL, which should find the record from the database with that id, and return the full URL.
EDIT: Rather than using the actual ID, you would want to create something like an index property in that schema. The index would be the total number of the records thus far, thus setting the zero-based index to the next open number.
Hope it helps. I did have fun with this challenge. The most difficult part for me was with the DNS package.
The thing is, as you learn the pattern, it’s much the same with ANY mongoose interaction. CRUD is dismayingly simple, when you wrap your head around it.
i have just managed to set up a schema which has originalUrl and a shortUrl… dont know but if it connects properly to the mlab link. Any way to check if it works or not the database at this stage currently?
also have got a post request working which i can then use dns.lookup to check url and then pass to database if it passes through dns.lookup function. will continue again tomorrow …
take a look at window.location.host or more broadly window.location on MDN.
Possibly window.location.hostname, unless you need to include any port number as part of the host URL (for example, my dev server uses port 3030 for my node server, if I wanted to include THAT, I’d use window.location.host as that does include the port number).
thanks ill try that… i was thinking mostly changing the location to required url would be enough to rerun location.address, hopefully.
What i was wondering was like in a get request the path route- it always begins with a “/” which specifies to the root path. Is there a way to change that to specify to the initial part… like for me it would be https://url-sh0rtn3r.glitch.me. or if its run locally can the path route access localhost:3000/
before the root route
Given a posted url, i would perhaps use a regex with a lookbehind and lookahead, and simply replace the capture group. You want the lookbehind to be your protocol, http:// or https:// or whatever, and your lookahead to the first slash after.
Crap. Your shortcut will likely be http, but urls can be all sorts: http, https, ftp…
app.get( “path”, function(to get executed when the path is hit))
so the path is always starting from " / " then comes the lookahead. can i type something in there so that when i type the lookbehind in the url bar the request-function gets called