ใคใณใฟใผใณใทใใใฎ็ต้จใใใญใฐใซๆธใใใจใ่ฆ่ซใใใฆใใใใใใฎใงไธๆธใใใใใซๆธใใใ
ไปฅไธใฏใใใซใฆใงใข (ๅพ่ฟฐ) ใ็จใใชใใ /
ใธใฎ POST ใ่ชญใใงใ ใใใใๅซใใฆๅใๅใฃใใใผใฟใใใฎใพใพ่ฟใใ ใใฎใตใผใ
express = require 'express'
app = express()
app.post '/', (req, res) ->
buf = ''
req.on 'data', (chunk) ->
buf += chunk
req.on 'end', ->
res.end buf
app.listen 3030, (->)
ใใใๅใใใฆใ curl ใณใใณใใงใฎใชใฏใจในใใฎ้ฃใฐใๆนใใใใคใ็ดนไปใใ:
echo HOGEHOGE | curl -X POST -F 'hoge=<-' localhost:3030
--------------------------1bee3ed01c9645db
Content-Disposition: form-data; name="hoge"
HOGEHOGE
--------------------------1bee3ed01c9645db--
echo HOGEHOGE | curl -X POST -F 'hoge=@-' localhost:3030
--------------------------be462c8006a6ea41
Content-Disposition: form-data; name="hoge"; filename="-"
Content-Type: application/octet-stream
HOGEHOGE
--------------------------be462c8006a6ea41--
curl -X POST -d hoge=fuga localhost:3030
hoge=fuga
curl -X POST -d name=cympfh -d pwd=koregacympfhnopwd localhost:3030
name=cympfh&pwd=koregacympfhnopwd
ใชใใปใฉใ่ชๅใงใใผใตใๆธใใฎใฏ่พใใใงใใ
express = require 'express'
bodyParser = require 'body-parser'
app = express()
app.use bodyParser.urlencoded { extended: false }
app.use bodyParser.json()
app.post '/', (req, res) ->
res.json req.body
app.listen 3030, (->)
echo HOGEHOGE | curl -X POST -F 'hoge=<-' localhost:3030
{}
-F
ใง้ใ form-data ใฏใฉใใใ express ใงใฏ็ก่ฆใใใใใใ? (cf. Node.jsใฎExpressใงmultipart/form-dataใๅฆ็ใใๆนๆณ - himetani's blog)
curl -X POST -d 'name=cympfh' localhost:3030
{"name":"cympfh"}
curl -X POST -d 'name=cympfh&password=myStr0ngPassWord' localhost:3030
{"name":"cympfh","password":"myStr0ngPassWord"}
nodeใงๅใใใจใฏใคใพใ้ๅๆ็ๅฆ็ใๆนฏๆฐดใฎใใใซ่กใใใจ
express = require 'express'
bodyParser = require 'body-parser'
app = express()
app.use bodyParser.urlencoded { extended: false }
app.use bodyParser.json()
asyncFn = (cont) ->
setTimeout (-> cont { ok: 'ok' }), 2000
app.post '/', (req, res) ->
asyncFn (data) ->
res.json data
app.listen 3030, (->)
ใใฆใฮท-ๅคๆใจใฏใ(lambda (x) (f x))
ใจ f
ใ็ญไพกใงใใใใจใใใ. CoffeeScript ใชใใ JavaScript ใงใฏใฉใใ
express = require 'express'
bodyParser = require 'body-parser'
app = express()
app.use bodyParser.urlencoded { extended: false }
app.use bodyParser.json()
asyncFn = (cont) ->
setTimeout (-> cont { ok: 'ok' }), 2000
app.post '/', (req, res) ->
asyncFn res.json # i found a bug here
app.listen 3030, (->)
ใใใๅฎ่กใใ้ใซ้ฉๅใชPOSTใๆใใใจใ ใใใฏๆฌกใฎใใใชใจใฉใผใๅใใฆๆญปใฌ:
/usr/local/lib/node_modules/express/lib/response.js:226
var replacer = app.get('json replacer');
^
TypeError: Cannot call method 'get' of undefined
at json (/usr/local/lib/node_modules/express/lib/response.js:226:22)
at [object Object]._onTimeout (/Users/cympfh/test/test.coffee:9:18)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
ๆ็ต็บ่กจใฎๆฅใ ็งใใฎใใณใไผๅ ดใซ็ใใฆใ ้ฏใไฝใ่ฟใใฆใใชใใจใกใณใใซๆๆใใใ ใใใใใญใฐใใฟใใจใใใชใจใฉใผใ ใฃใใใใ ๅ้กใฎ็ฎๆใ่ฆใคใใใซ็ธๅฝใฎ่ฆๅดใ่ฆใใใ (ใใใฃใฆใใใใงใใใ?)
asyncFn = (res) ->
setTimeout (-> res.json { ok: 'ok' }), 2000
app.post '/', (req, res) ->
asyncFn res
ใใใฏใใพใๅใใ ใใใใใใใ res.json
ใฏresใจใใใชใใธใงใฏใใฎใกใฝใใใ่ชญใใง ใใใฏใใคใใใใฏในใณใผใใชใฎใ๏ผ
Obj =
i: 1
read: -> @i
console.warn Obj.read() # 1
async = (fn) ->
setTimeout (-> console.warn fn()), 2000
async Obj.read # undefined
ๆ่จใฏใ ใฉใ ใ่จ็ฎใฎใฌใใซใงๆใ็ซใคใใจใๅฅใช่จ่ชใงๆใ็ซใใชใใใฃใฆใ ่ชฐใๆจใใงใใใใชใใจใใใใจใงใใ. ๆใใใซใๆฎ้ใใญใฐใฉใใณใฐ่จ่ชใจๅผใฐใใใใฎใฏใฉใ ใๅผใใใ้ซใ่กจ็พๅใๆใค. ไพใใฐใใค่จ็ฎใฏใฉใ ใ้ ใ่กจ็พใงใใใใใไปฅไธใฎ่กจ็พๅใๆใค. (ไธ่ฌใซ) (ๅใฎ็กใ) ใใค่จ็ฎใงใฏใจใผใฟๅคๆใฏๆ็ซใใชใ.