Code Monkey home page Code Monkey logo

Comments (5)

dougwilson avatar dougwilson commented on July 20, 2024 2

Since Express is just a sugar layer on top of Node.js server, the example from the README is easily switched to Express. For example:

var auth = require('basic-auth')
var express = require('express')

// Create app
var app = express()

app.use(function (req, res, next) {
  var credentials = auth(req)

  if (!credentials || credentials.name !== 'john' || credentials.pass !== 'secret') {
    res.status(401)
    res.header('WWW-Authenticate', 'Basic realm="example"')
    res.send('Access denied')
  } else {
    next()
  }
})

// More routes here

// Listen
app.listen(3000)

from basic-auth.

blakeembrey avatar blakeembrey commented on July 20, 2024

The examples are using the http module built in node, described https://github.com/jshttp/basic-auth#with-vanilla-nodejs-http-server. If you want something that works as middleware, try https://github.com/expressjs/basic-auth-connect.

from basic-auth.

jesgundy avatar jesgundy commented on July 20, 2024

Super helpful answer - would consider putting in the Readme, fwiw.

from basic-auth.

dougwilson avatar dougwilson commented on July 20, 2024

Since this is bot an express specific module, I would rather not, as otherwise the README will turn into a giant list of examples for every conceivable framework out there.

from basic-auth.

jesgundy avatar jesgundy commented on July 20, 2024

from basic-auth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.