npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-reuse

v1.0.2

Published

Lightweight express app framework that makes using and creating reusable routes easy.

Downloads

2

Readme

express-reuse

A lightweight framework that makes reusing pieces of your express app easy. More detailed documentation available here

Getting Started

The simplest way to use express-reuse is to install a strategy. For now, let's use express-reuse-local-login.

$ npm install express-reuse
$ npm install express-reuse-local-login

Steps:

  1. Set up necessary middleware
  2. Configure Email (optional)
  3. Connect to your database
  4. Put it all together

Set up necessary middleware

express-reuse requires you to use at least body-parser, cookie-parser, and some sort of session handling middleware. In this case we're using express-session.

app.use(cookieParser())
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
app.use(expressSession({
  secret : /* Your session secret */ ,

  //these can be true or false
  resave: true,
  saveUninitialized: true,
  cookie: {
    secure: true
  }
}))

Configure Email

This step is optional. If you don't want to use transactional emails, simply include noEmail: true in the options object when we set up express-reuse.

For a more detailed explanation on what can be done here, see the documentation for email-templates. The emailOptions object we create in this section gets passed directly to email-templates.

First we need a nodemailer transport. This example uses ethereal.email.

var transporter = nodemailer.createTransport({
  host: 'smtp.ethereal.email',
  port: 587,
  auth: {
    user: /* your email    */,
    pass: /* your password */
  }
})

Next we need to setup a folder with our email templates. The templates we need will depend on the strategy being used. In this case, express-reuse-local-login requires four templates.

emails
  - emailConfirm
  - emailConfirmThankYou
  - forgotPassword
  - passwordChanged

See the email-templates documentation for a more detailed explanation about how to set these up.

Finally, we create our emailOptions object.

var emailOptions = {
  views: {
    root: /* directory containing email templates */
  },
  message: {
    from: "[email protected]"
  },
  transport: transporter
}

Connect to database

Out of the box, express-reuse works with mongoose. But it can be easily configured to work with any database you want. If you want to work with another database module, see the documentation here.

First, connect to the database.

mongoose.connect( /* Your database URL */)
  .then(() => {
    console.log("Database is connected")
  })
  .catch((err) => {
    console.log("Can not connect to the database" + err)
  })

Then create a mongoose User model. The fields that need to be in the model will depend on the particular strategy being used. For express-reuse-local-login your schema will look like this.

var userSchema = new mongoose.Schema({
  email:                {type: String,  select: false},
  password:             {type: String,  select: false},

  //optional if not using transactional emails
  confirmEmailToken:    {type: String,  select: false},
  resetPasswordToken:   {type: String,  select: false},
  resetPasswordExpires: {type: Date,    select: false},
  emailConfirmed:       {type: Boolean, select: false, default: false},

  /* Other fields related specifically to your app */
})

var User = mongoose.model("User", userSchema)

It is not required that the fields be set to select:false. This just shows that the strategy will work correctly even if you do.

Put it all together

Now we're ready to setup express-reuse. First require the packages:

var expressReuse           = require('express-reuse')
var expressReuseLocalLogin = require('express-reuse-local-login')

Then we tell express-reuse to use the local login strategy.

expressReuse.useStrategy(expressReuseLocalLogin)

Now we app.use() and provide our options object to express-reuse. The options object contains the User model and emailOptions we set up earlier.

app.use("/auth", expressReuse({
  userModel: User,

  //For transactional emails
  emailOptions: emailOptions

  //For no emails
  noEmail: true
})

Customization

express-reuse is fully configurable and customizable. For more information, see the documentation.