Last active
January 2, 2016 03:59
-
-
Save aaylward/8247506 to your computer and use it in GitHub Desktop.
example grunt express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(grunt) { | |
require('load-grunt-tasks')(grunt); | |
grunt.loadNpmTasks('grunt-express-server'); | |
grunt.initConfig({ | |
pkg: require('./package.json'), | |
express: { | |
dev: { | |
options: { | |
script: './server.js' | |
} | |
} | |
}, | |
watch: { | |
options: { | |
livereload: true | |
}, | |
express: { | |
files: ['**/*.json', '**/*.js'], | |
tasks: ['express:dev'], | |
options: { | |
spawn: false | |
} | |
} | |
} | |
}); | |
grunt.registerTask('dev', | |
['express:dev', 'watch'] | |
); | |
grunt.registerTask('default', ['dev']); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "weee", | |
"version": "0.0.0", | |
"description": "", | |
"main": "Gruntfile.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node server.js" | |
}, | |
"author": "", | |
"license": "BSD-2-Clause", | |
"dependencies": { | |
"q-io": "~1.10.6", | |
"q": "~0.9.7" | |
}, | |
"devDependencies": { | |
"grunt-express-server": "~0.4.11", | |
"grunt": "~0.4.2", | |
"grunt-contrib-watch": "~0.5.3" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"weeee": true, | |
"yesss": "yessss" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var fs = require("q-io/fs"); | |
var Q = require('q'); | |
Q.longStackSupport = true; | |
var app = express(); | |
var BASE_PATH_RE = /\/pathstuff\/morepath\/(.+)/ | |
var RESOURCE_BASEDIR = './' | |
var CACHE = {}; | |
var PORT = 8082; | |
function readFilePromise(filename) { | |
return fs.read(RESOURCE_BASEDIR + filename).then(function(content) { | |
return CACHE[filename] = content.toString(); | |
}); | |
} | |
function getResourcePromise(filename) { | |
if (filename in CACHE) { | |
console.log('reading file from cache...'); | |
return Q(CACHE[filename]); | |
} | |
console.log('reading file from fs...'); | |
return readFilePromise(filename); | |
} | |
app.get(BASE_PATH_RE, function(req, res) { | |
var filename = req.params[0]; | |
getResourcePromise(filename).then(function(response) { | |
res.contentType(filename); | |
res.send(response); | |
}).fail(function(err) { | |
console.log("[ERROR]: ", JSON.stringify(err)); | |
res.send(404, 404); | |
}).done(); | |
}); | |
app.listen(PORT); | |
console.log('starting server on ' + PORT); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment