Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Statik

A simple and easy-to-use Node.js module to server static files over HTTP. It's
A simple and easy-to-use Node.js module to server static files over HTTP. Now with authentication option. It's
super simple to use it.

## Command line usage
Expand Down Expand Up @@ -30,6 +30,9 @@ $ statik --port 3000 ~/Sites/project
* **hidden**: allow transfer of hidden files. Defaults to false
* **redirect**: redirect to trailing "/" when pathname is directory. Defaults to true
* **compress**: enable gzip compression. Defaults to true
* **auth**: enable http-authentication. Defaults to false
* **authRealm**: set window title for http-authentication
* **authFile**: path to htpasswd file
* **verbose**: enable logging to stdout. Defaults to false

## Use it programmatically
Expand All @@ -51,19 +54,20 @@ serving `./public` directory.

### Customisations

You can specify the directory you wish to serve as an argument.
You can specify the directory you wish to serve as an argument. And/or set restricted access to website, if necessary.

```javascript
// app.js
var statik = require('statik');
statik({
port: 3000,
root: '/Users/hongymagic/Sites'
root: '/Users/hongymagic/Sites',
auth: true
});
```

Your server will be running on [http://localhost:3000/](http://localhost:3000/)
server `/Users/hongymagic/sites` directory.
server `/Users/hongymagic/sites` directory. All users will be prompted for login/password to get access to your site.

You can also use command line options when invoking `statik` function.

Expand Down
6 changes: 6 additions & 0 deletions bin/statik
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var usage = [
' --hidden Allow hidden files to be served. Defaults to false.',
' --redirect Redirect to trailing "/" when pathname is directory. Defaults to true.',
' --maxAge Browser cache maxAge in milliseconds. Defaults to 0.',
' --auth Enable http-authentication. Defaults to false.',
' --authRealm Window title for http-authentication. Defaults to "Restricted realm".',
' --authFile Path to file with passwords. Defaults to "/../data/users.htpasswd".',
' --verbose Enable verbose logging to stdout. Defaults to false.'
].join('\n');

Expand All @@ -25,6 +28,9 @@ var known = {
"hidden": Boolean,
"redirect": Boolean,
"compress": Boolean,
"auth": Boolean,
"authRealm": String,
"authFile": String,
"verbose": Boolean,
"help": Boolean
};
Expand Down
12 changes: 0 additions & 12 deletions examples/basic-port.js

This file was deleted.

7 changes: 3 additions & 4 deletions examples/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
// ## Barebones demonstration.
//
// The following code will serve everything inside `./public` over HTTP using
// port 1203 (default).
// port 3000 (default).
//

var statik = require('../index');
var server = statik.createServer();
statik(); // just run the server

server.listen();
console.log("server online at http://localhost:1203/");
console.log("Server online at http://localhost:3000/");
17 changes: 17 additions & 0 deletions examples/complex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// ## Barebones demonstration.
//
// The following code will serve everything inside `./public` over HTTP using
// port 3000.
//

var statik = require('../index');
statik({
port: 3000,
auth: true,
authRealm: 'My over-strictly forbidden area. Don`t touch ever!',
authFile: __dirname + "/users.htpasswd", // admin:123456 ;-)
verbose: true
});

console.log("Server with auth online at http://localhost:3000/");
2 changes: 1 addition & 1 deletion examples/public/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title>This is a sub folder</title>
</head>
<body>
<p>Of course it works!</p>
<p>Of course it works in subfolder!</p>

<script src="../scripts/app.js"></script>
</body>
Expand Down
1 change: 1 addition & 0 deletions examples/users.htpasswd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
admin:123456
21 changes: 18 additions & 3 deletions lib/statik.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var path = require('path');
var extend = require('extend');
var connect = require('connect');
var path = require('path'),
auth = require('http-auth'),
extend = require('extend'),
connect = require('connect');

var defaults = {
/* connect options */
port: 3000,
Expand All @@ -11,6 +13,11 @@ var defaults = {
hidden: false,
redirect: true,

/* auth opions */
auth: false,
authRealm: 'Restricted realm',
authFile: __dirname + '/../data/users.htpasswd',

/* other options */
compress: true,
verbose: false
Expand All @@ -29,6 +36,14 @@ module.exports = function (opts) {
app.use(connect.logger('short'));
}

if (options.auth) {
var basic = auth.basic({
realm: options.authRealm,
file: options.authFile
});
app.use(auth.connect(basic));
}

app.use(connect.static(options.root, options));

// start the app on given port. defaults to 3000
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"file",
"server",
"cdn",
"developement"
"developement",
"auth"
],
"repository": "git://github.com/hongymagic/statik",
"bugs": {
Expand All @@ -20,7 +21,8 @@
"dependencies": {
"extend": "~1.2.1",
"nopt": "~2.1.2",
"connect": "~2.11.0"
"connect": "~2.11.0",
"http-auth": "~2.1.5"
},
"devDependencies": {},
"main": "index",
Expand Down