Node.js Exporting and consuming modules edit

Building your own modules

You can also reference an object to publicly export and continuously append methods to that object:

const auth = module.exports = {}
const config = require('../config')
const request = require('request')

auth.email = function (data, callback) {
  // Authenticate with an email address
}

auth.facebook = function (data, callback) {
  // Authenticate with a Facebook account
}

auth.twitter = function (data, callback) {
  // Authenticate with a Twitter account
}

auth.slack = function (data, callback) {
  // Authenticate with a Slack account
}

auth.stack_overflow = function (data, callback) {
  // Authenticate with a Stack Overflow account
}

To use any of these, just require the module as you normally would:

const auth = require('./auth')

module.exports = function (req, res, next) {
  auth.facebook(req.body, function (err, user) {
    if (err) return next(err)

    req.user = user
    next()
  })
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Table Of Contents
2 npm
5 Exporting and consuming modules
42 cli
60 Hack
65 ES6
92 CORS
106 N-API
  ↑ ↓ to navigate     ↵ to select     Esc to close