// 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
Related Posts
More content you might like
Build a Custom Rate Limiter in Node.js with Redis
Have questions or want a follow-up tutorial? Leave a comment or reach out—we’d love to help.
Building a Real-Time Chat Application with WebSockets in Node.js
WebSockets provide a full-duplex communication channel over a single TCP connection, allowing both the client and server to send messages at any time. Unlike HTTP, WebSockets maintain a persistent connection, making them ideal for applications requiring real-time updates, such as chat apps, online gaming, and live notifications.
If you haven’t already installed Node.js, you can download it from the official website. Node.js includes npm, the package manager we will use to install dependencies.
JWT Token Creation and Verification in Node.js using 'jsonwebtoken'
No preview available for this content.
Simple HTTP Server in Node.js
No preview available for this content.
Discussion 0
Please sign in to join the discussion.
No comments yet. Be the first to share your thoughts!