Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Page Title in NodeJS

How can I change the title for each page using NodeJS?

The index.html (not .js file) that is inside public folder is:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
    <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
  <meta
     name="viewport"
    content="width=device-width, initial-scale=1, shrink-to-fit=no"
   />
   <meta name="theme-color" content="#000000" />

  <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />

  <title>Default Titlte for all pages</title>
</head>
<body>
   <noscript>You need to enable JavaScript to run this app.</noscript>
   <div id="root"></div>

 </body>
</html>

How can I change the title dynamically whenever I navigate to any page?

like image 456
key.m Avatar asked Oct 29 '25 02:10

key.m


1 Answers

It's nothing else than changing page title in javascript by default like document.title = 'title'; but it depends on your environment.

NodeJS itself lacks the ability to parse and transform HTML.

In express it would be like (a route based approach):

router.get('/', function(req, res, next) {
    res.render('index', { title: 'title' });
}); 
like image 158
Felix Avatar answered Oct 30 '25 18:10

Felix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!