set headers in express

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

// Step 1: Define a middleware function to set headers
const setHeaders = (req, res, next) => {
  // Step 2: Set the desired headers
  res.setHeader('Content-Type', 'application/json');
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');

  // Step 3: Continue to the next middleware in the stack
  next();
};

// Step 4: Use the middleware in the application
app.use(setHeaders);

// Step 5: Define your routes and other application logic below
app.get('/', (req, res) => {
  res.send('Hello, world!');
});

// Step 6: Start the server
const port = 3000;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});