With Express:
static.js
:
'use strict';
var express = require('express'),
app = express();
/**
// Cache Control
var oneDay = 86400000;
app.use(express.static(__dirname + '/public', { maxAge: oneDay }));
**/
app.use(express.compress());
app.use('/data', express.static(__dirname + '/data'));
app.get('/', function(req, res) {
res.sendfile('index.html', { root: __dirname });
});
var port = process.env.PORT || 8881;
var server = app.listen(port);
console.log("Serving!");
package.js
:
{
"name": "static",
"version": "0.0.1",
"dependencies": {
"express": "^3.18.4"
},
"engines": {
"node": "0.10.x"
}
}
./data
directory with files in local dir, and an index.html
Note: will not serve files recursively, only 1 level
Note: Makes a killer microservice for providing static assets to some service, extremely fast, and much less configuration than nginx or Apache with a caching proxy, etc...
Simpler Example:
$ npm install connect serve-static
var connect = require('connect'),
serveStatic = require('serve-static');
var server = connect();
server.use(serveStatic("./"));
server.listen(process.env.PORT || 8881);
This will start a static server from the root
directory on provided port