Last active
December 16, 2015 10:39
-
-
Save bathtimefish/5421469 to your computer and use it in GitHub Desktop.
Yeoman開発用 APIプロキシ。YeomanでREST APIサーバーとフロントエンドを同時に開発する際に使用する。
node-http-proxyのインストールが必要 'npm install http-proxy'
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
define(['jquery'], ($)-> | |
'use strict' | |
$.getJSON('api/items/', (data)-> | |
ul = document.querySelector('div.hero-unit > ul') | |
for row in data | |
li = document.createElement 'li' | |
txt = document.createTextNode "#{row.id}, #{row.user}, #{row.message}" | |
li.appendChild txt | |
ul.appendChild li | |
) | |
'loading api data.' | |
) |
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
httpProxy = require('http-proxy') | |
apiPath = '/api' | |
httpProxy.createServer((req, res, proxy) -> | |
if req.url.indexOf(apiPath) is 0 | |
proxy.proxyRequest(req, res, | |
host: 'localhost', | |
port: 3000 | |
) | |
else | |
proxy.proxyRequest(req, res, | |
host: 'localhost', | |
port: 9000 | |
) | |
).listen(8000) | |
console.log("Starting Server at http://localhost:8000/") |
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
{ | |
"simulated-lag": 1000, | |
"cors": false, | |
"jsonp": false, | |
"proxy": { | |
"server": "http://api.realserver.com", | |
"default": false | |
}, | |
"variables": { | |
"server": "http://api.realserver.com" | |
}, | |
"routes": [ | |
"/items/:itemid" | |
] | |
} |
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
[ | |
{ "id": 1, "user": "john", "message": "hello" }, | |
{ "id": 2, "user": "bob", "message": "Hi!" }, | |
{ "id": 3, "user": "mike", "message": "good bye." } | |
] |
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
<div class="container"> | |
<div class="hero-unit"> | |
<h1>API Data</h1> | |
<ul> | |
</ul> | |
<h3>Enjoy coding! - Yeoman</h3> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment