Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How do I create a search engine (an extension of work from the Express, Pug authentication course)

I have completed the Express and Pug Course for creating an authentication system for registration and log in. I really enjoyed the course, and I'm now trying to extend my work.

I have created all the CRUD applications (PUT,DELETE,POST,GET) successfully, but how do I add a search engine, so the user can perform a search via a search engine? Not via the server on MongoDB.

Any help and advice for me (as a newbie) would be greatful.

Thanks

2 Answers

Samuel Ferree
Samuel Ferree
31,722 Points

When the user runs a search, they are sending a search to the server. The server would take that search (whatever text they've entered) and use it to search the database, and then display a list of results that meet the criteria.

So first, you'd write a custom javascript object/function/class that knows how to take a string of text, and search your database. It's custom because how it does this, extracting keywords etc depends on the structure of your data.

Once you have that, in express you'd have something like

app.get('/search/:query', function(req, res) {
  let results = searchEngine.runSearch(req.query);
  res.render('results', { results: results });
});

Then you just need a pug template for the search page (with the text input) and the results page.

Thank you Samuel, that's really helped!

Kevin Becerra
Kevin Becerra
14,243 Points

There's some pretty good modules on npm, but honestly if you're new to Javascript I wouldn't just be diving into all this so fast. I recommend you get used to writing plain Javascript first to get the fundamentals down on the language. If you don't your coding process will be really slow when you're jumping back and fourth a lot trying to find answers either in documentation or like in stack-overflow. Those two resources help out a lot when figuring out difficult problems, but if you wanna be efficient with your code, starting with some of the more basic concepts helps you the most overall.