Skip to content

Instantly share code, notes, and snippets.

@bathtimefish
Last active December 16, 2015 10:39
Show Gist options
  • Save bathtimefish/5421469 to your computer and use it in GitHub Desktop.
Save bathtimefish/5421469 to your computer and use it in GitHub Desktop.
Yeoman開発用 APIプロキシ。YeomanでREST APIサーバーとフロントエンドを同時に開発する際に使用する。 node-http-proxyのインストールが必要 'npm install http-proxy'
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.'
)
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/")
{
"simulated-lag": 1000,
"cors": false,
"jsonp": false,
"proxy": {
"server": "http://api.realserver.com",
"default": false
},
"variables": {
"server": "http://api.realserver.com"
},
"routes": [
"/items/:itemid"
]
}
[
{ "id": 1, "user": "john", "message": "hello" },
{ "id": 2, "user": "bob", "message": "Hi!" },
{ "id": 3, "user": "mike", "message": "good bye." }
]
<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