// 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');
});Basic Authentication using 'express-basic-auth' middleware
javascript
Related Posts
More content you might like
Tutorial
Build a Custom Rate Limiter in Node.js with Redis
Use Postman or curl:
curl http://localhost:3000Apr 04, 2025
Read More Tutorial
javascript css +1
Building a Real-Time Chat Application with WebSockets in Node.js
npm init -ynpm install express socket.ioAug 03, 2024
Read More Code
php
JWT Token Creation and Verification in Node.js using 'jsonwebtoken'
No preview available for this content.
Jan 26, 2024
Read More Code
javascript
Simple HTTP Server in Node.js
No preview available for this content.
Jan 26, 2024
Read MoreDiscussion 0
Please sign in to join the discussion.
No comments yet. Be the first to share your thoughts!