Expressjs with hbs (handlebars)

Expressjs with hbs (handlebars)

server.js

const express = require('express')
const hbs = require('hbs');

const app = express()
const port = 3000

// Set the view engine to 'hbs'
app.set('view engine', 'hbs');


// Set the directory where your views (Handlebars templates) are located
app.set('views',__dirname+'/views');


// Create a basic route that renders a Handlebars template
app.get('/', (req, res) => {
  // Data to pass to the template
  const data = {
    pageTitle: 'Express with Handlebars',
    welcomeMessage: 'Welcome to Express.js with Handlebars!'
  };

  // Render the 'index' template and pass in the data
  res.render('index', data);
});

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

  res.send('Hello World!d')
   // Execute npm run build command in the target directory

})



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

  res.send('Got a POST request')

})

app.listen(port, () => {

  console.log(`Example app listening on port ${port}`)

})

views/index.hbs

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{{pageTitle}}</title>
</head>
<body>
    <header>
        <h1>{{pageTitle}}</h1>
    </header>
    <main>
        <p>{{welcomeMessage}}</p>
    </main>
</body>
</html>