Published on January 26, 2024By DeveloperBreeze

// Import required modules
const express = require('express');
const app = express();
const basicAuth = require('express-basic-auth');

// Define a user for basic authentication
const users = { 'username': 'password' };

// Use express-basic-auth middleware for authentication
app.use(basicAuth({ users }));

// Define a route accessible only to authenticated users
app.get('/secure', (req, res) => {
  res.send('Authenticated route');
});

Comments

Please log in to leave a comment.

Continue Reading:

Basic Authentication

Published on January 26, 2024

php

Simple WebSocket Server using 'ws' library

Published on January 26, 2024

javascript

File Stream Copy using 'fs' module

Published on January 26, 2024

javascript

Execute Shell Command using 'child_process' module

Published on January 26, 2024

javascript

Parse URL and Query Parameters

Published on January 26, 2024

javascript

Hashing Password with SHA-256 using 'crypto' module

Published on January 26, 2024

javascript

Create and Print Buffer

Published on January 26, 2024

javascript

Construct File Path using 'path' module

Published on January 26, 2024

javascript

Event Emitter using 'events' module

Published on January 26, 2024

javascript

Set and Access Environment Variable

Published on January 26, 2024

javascript