Last active
August 29, 2015 14:02
-
-
Save glassesfactory/49ed0f0d68cbd5003e1b to your computer and use it in GitHub Desktop.
なんか gulp で動く特定ディレクトリをかっさらって適当に crud な API 作ってくれる君
This file contains 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
express = require 'express' | |
fs = require 'fs' | |
path = require 'path' | |
gulp = require 'gulp' | |
configure = require "../../apimock/configure" | |
app = express() | |
configure app, {} | |
current = path.join process.cwd(), 'path/to/resources' | |
routes = {} | |
checkDirectory =(current, prefix)-> | |
resources = fs.readdirSync current | |
tmp = {} | |
for file in resources | |
if fs.statSync(path.join(current, file)).isDirectory() | |
newPrefix = file | |
newPrefix = path.join prefix, file if prefix | |
checkDirectory path.join(current, file), newPrefix | |
else | |
prefix = "/" unless prefix | |
tmp[prefix] = [] unless tmp.hasOwnProperty prefix | |
tmp[prefix].push file | |
routes[key] = tmp[key] for key of tmp | |
return | |
checkDirectory current | |
addRoutes = (url, mod)-> | |
app.get "/" + url, mod.getIndex if mod.hasOwnProperty "getIndex" | |
app.get path.join(url, ":id"), mod.get if mod.hasOwnProperty "get" | |
app.post url, mod.post if mod.hasOwnProperty "post" | |
app.put path.join(url, ":id"), mod.put if mod.hasOwnProperty "put" | |
app.delete path.join(url, ":id"), mod.delete if mod.hasOwnProperty "delete" | |
return | |
binder = (routes)-> | |
for key of routes | |
files = routes[key] | |
for file in files | |
filePath = path.join(key, file) | |
mod = require path.join(current, filePath.replace('/\.(js|coffee)^/', '')) | |
addRoutes key, mod | |
binder routes | |
gulp.task "express", -> | |
app.listen(3000) | |
This file contains 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
#resource/api/tests/getIndex.coffee | |
exports.getIndex =(req, res)-> | |
return res.json { "pharahe" : "٩( ᐛ )و" } | |
# -> http://127.0.0.1:3000/api/tests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ソース整理し忘れた