skip to Main Content

I’ve started to learn node.js and express, and I am curious what is the point of all this routing. Example:

app.get('/', (req, res) => {
    
    res.render('index', { title: 'Home', blogs });

});

app.get('/about', (req, res) => {

    
    res.render('about', { title: 'About' });


});

What’s wrong with normal <a href>tag?

2

Answers


  1. Nothing wrong with any above, you are just mixing up things from different worlds.

    Express.js routes you are mentioning are declarations for server to instruct which code to execute when browser open your website on a particular url.

    <a href> tag is used in html to instruct browser where to navigate if user click it.

    Giving your example:
    If you have somewhere in your html <a href="/about">About page</a. and user clicks it, then browser would navigate to this page.
    Express.js will receive new request from browser, render "about" template and return back to user’s browser.

    Login or Signup to reply.
  2. The <a> HTML element (or anchor element), with its href attribute, creates a hyperlink to web pages, files, email addresses, locations in the same page, or anything else a URL can address.

    Express routes if you want a simple answer are the place where you receice new request from users (browser), and determine how the server should respond to those requests.

    and you need to put in your mind that when a user clicks on a link <a href>, the browser send a new request to the server for the linked page.

    I hope I simplified the answer a bit, I hope you understand, I also had the same problem between these two things, but you will understand it more when you start a small project.

    good luck,

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search