A super-simple node-static server with http basic auth, perfect for serving static content on Heroku behind a username and password.
Works as a simple static server without authentication until pushed to production, whereupon basic HTTP authentication is employed.
Supports a superset of the node-static
API.
var server = require('auth-static')
server(config)
Auth-static uses a configuration object with the following keys:
var config = {
options: {
// supports node-static options
},
password: process.env.PASSWORD, // environment variable
port: 1234, // port for localhost only
realm: 'Private', // label for the auth form
root: './example', // root directory to serve
username: process.env.USERNAME // environment variable
}
The simplest way to get up and running on with auth-static on Heroku (assuming you've already installed the Heroku Toolbelt) is to:
- Create a Heroku app
- Install
auth-static
via NPM - Create the server
- Tell Heroku to run the server
- Set up the config variables in the Heroku Dashboard
- Commit your changes
- Push your code to Heroku
- Test your app
heroku create your-app-name
Replace your-app-name
with whatever you want your app to be called. This example would result in the app being available at https://your-app-name.herokuapp.com/.
npm install --save auth-static
// index.js
var server = require('auth-static')
server({
options: {
cache: 3600,
gzip: true
},
password: process.env.PASSWORD,
port: 1234,
realm: 'Private',
root: './example',
username: process.env.USERNAME
})
The Procfile tells Heroku what to do once it's installed your project.
We want Heroku to run the server that lives in index.js
:
// Procfile
web: node index.js
Visit https://dashboard.herokuapp.com/, navigate to the settings page for your app and edit the config variables. We need to create pairs for NODE_ENV
, USERNAME
, and PASSWORD
. NODE_ENV
needs a value of productio
for everything to work, whereas the the other two can be whatever you want.
git commit -m "Setting up auth-static"
git push master heroku
Running heroku open
will open your app (e.g. https://your-app-name.herokuapp.com/) in your browser of choice. If everything has worked, you will be presented with a basic auth form; enter your username and passowrd and you'll be able to see your static content.