DEV Community

Ray
Ray

Posted on

Lab 6 After a Break

Well not much of a break.

This week was filled with a couple of firsts. The first week of my new job and the first time using most of the programs introduced this week. It was also the first time I'll be working with Telescope and the first time using git diffs and GitHub Gist. Both of which I'm not terribly sure if I have a full grasp of yet.

The Set Up

So for Lab 6, we were meant to set up Seneca's Telescope blog aggregator and to pump the links we get into our Link Checker apps. At least, we were meant to develop a proof of concept for the feature and upload the diff to GitHub Gist.

Seems easy, right? Well, it requires a little more effort than that.

In order to set up Telescope we need 3 things: redis-server, Elasticsearch, and Docker. In order to properly run redis-server, we must also get WSL, a Linux subsystem for Windows.

WSL was a slight pain to get working but now that I have it I think I might use it forever. Running a dual boot system for a while just to develop web apps because so cumbersome that I gave up early last year and just used Windows. But with WSL I can run a Ubuntu terminal in vscode and kill two birds with one stone. The only thing I have to look out for is accidentally having like 5 terminals open at once which... I'm doing right now to gather screenshots for this blog post.

It's up!

So redis-server is up, next is Elasticsearch. Now, I'm not too sure where I went wrong here but after properly installing Elasticsearch and getting it running properly, as well as verifying that all the ports were the same between them, I just couldn't get Elasticsearch to properly connect with the Telescope backend. This is unfortunate because I'd really like to get it properly working before I take a dive into Telescope for real. However, for just this lab, I was able to set up a limited Elasticsearch server to grab some blog posts from the API and help me with my work.

Finally, Docker. I'm not too sure where Docker fits into everything because I ran everything separately. However, I think I'll try to get it all together in a Docker container for the future as it seems like it'll make development a lot easier. I had an issue with setting up Docker with my WSL that involved having the Hyper-V VM running which made Docker crash. Weird stuff, but after a bit of research, it was easy to fix.

A bit of coding

The objective is this: grab links to blog posts from the Telescope backend API, pipe them into a text file, grab the links from that text file, and finally test them to make sure they're good links like my app already does.

Simple, right?

This is the part where I say no, but actually it is.

index.js excerpt

if (options.t) {

    var url = 'http://localhost:3000/posts/';

    req.get({
        url: url,
        json: true,
        headers: {
            'User-Agent': 'request'
        }
    }, (err, res, data) => {
        if (err) {
            console.log('Error:', err);
        } else if (res.statusCode !== 200) {
            console.log('localhost did not respond... check to see if your local telescope session is up...');
        } else {
            const urls = data.map(e => url + e.id)

            fs.writeFile('files/telescope.txt', JSON.stringify(urls), function (err) {
                if (err) throw err;
              }); 
        }
    });
}
Enter fullscreen mode Exit fullscreen mode

The above code basically queries the local version of the Telescope API for the post ids, then we just stick the rest of the url to the front of the id, then write it straight into a file.

more index.js excerpts

if(options.t){
    filename = 'files/telescope.txt'
}
else {
        filename = `${argv[2]}`;
}
fs.readFile(filename, (err, fileContents) => {
    try {
        var linkList = generateLinkList(fileContents);
        linkList = Array.from(linkList);
    } catch (err) {
        console.log("The app has recieved a wrong filename.")
        console.log("Please enter a correct filename.")
        exit(1);
    }
    validateLinks(linkList)
})
Enter fullscreen mode Exit fullscreen mode

This is also a simple change to the rest of the code, writing an if statement to check if we've used our telescope argument, and setting the file to that if so! Easy.

Frankly, I just had to add some connections to the local API and modify almost nothing else.

I want to be clear that this code is a proof of concept as outline in the Lab 6 instructions, it's not mean to be comprehensive and perfect for every situation (for example, I could have it query a backend API that I host on a Heroku server or something so that I won't have to set up the local server every time to use it), but it works well for what it is. I can't wait to expand upon this feature and fix up the code to be a lot more modular, which I think is going to be my next big refactor.

Top comments (0)