簡體   English   中英

Node.js為heroku構建json api

[英]Node.js build json api for heroku

我想通過node.js構建API,它在本地正常工作,但在雲中失敗。 你能幫我找到這個bug嗎? 謝謝

https://infinite-gorge-6020.herokuapp.com

app.get('/', function(req, res) {
  res.render('index.ejs');
});

app.get('/host', function(req, res) {
  var sessionId = app.get('sessionId'),
      // generate a fresh token for this client
      token = opentok.generateToken(sessionId, { role: 'moderator' });

  res.json('host.ejs', {
    apiKey: apiKey,
    sessionId: sessionId,
    token: token
  });

日志

2014-11-26T03:25:38.289313+00:00 heroku[router]: at=info method=GET path="/css/sample.css" host=infinite-gorge-6020.herokuapp.com request_id=18d8ce80-53a3-487f-94f5-a45f984bad67 fwd="1.171.57.36" dyno=web.1 connect=4ms service=6ms status=200 bytes=720
2014-11-26T03:25:39.207199+00:00 heroku[router]: http_error="Invalid HTTP status line" at=error code=H17 desc="Poorly formatted HTTP response" method=GET path="/host" host=infinite-gorge-6020.herokuapp.com request_id=a56aaf2e-7d2b-4479-bc01-3efd9542432b fwd="1.171.57.36" dyno=web.1 connect=1ms service=3ms status=503 bytes=639
2014-11-26T03:30:50+00:00 heroku[slug-compiler]: Slug compilation started
2014-11-26T03:30:55+00:00 heroku[slug-compiler]: Slug compilation finished
2014-11-26T03:30:55.675999+00:00 heroku[api]: Deploy cd3d6fe by kenshin0812@hotmail.com
2014-11-26T03:30:55.675999+00:00 heroku[api]: Release v16 created by kenshin0812@hotmail.com
2014-11-26T03:30:55.943650+00:00 heroku[web.1]: State changed from up to starting
2014-11-26T03:30:57.789085+00:00 heroku[web.1]: Starting process with command `node index.js`
2014-11-26T03:30:58.927452+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2014-11-26T03:30:59.218593+00:00 heroku[web.1]: State changed from starting to up
2014-11-26T03:30:59.079473+00:00 app[web.1]: Listening on , server_port 14730
2014-11-26T03:31:00.557311+00:00 heroku[web.1]: Process exited with status 143
2014-11-26T03:31:06.268233+00:00 heroku[router]: at=info method=GET path="/" host=infinite-gorge-6020.herokuapp.com request_id=824385d9-4aa9-439d-86a4-e8693d080e6b fwd="1.171.57.36" dyno=web.1 connect=1ms service=46ms status=200 bytes=2770
2014-11-26T03:31:06.529761+00:00 heroku[router]: at=info method=GET path="/css/sample.css" host=infinite-gorge-6020.herokuapp.com request_id=1f19e9ee-008f-4a33-b66a-bbd7f6b75bea fwd="1.171.57.36" dyno=web.1 connect=5ms service=9ms status=200 bytes=720
2014-11-26T03:31:08.043604+00:00 heroku[router]: http_error="Invalid HTTP status line" at=error code=H17 desc="Poorly formatted HTTP response" method=GET path="/host" host=infinite-gorge-6020.herokuapp.com request_id=bd19cbbe-bc58-4d4c-8426-d016e29555e1 fwd="1.171.57.36" dyno=web.1 connect=1ms service=6ms status=503 bytes=635
2014-11-26T03:31:08.041211+00:00 app[web.1]: Wed, 26 Nov 2014 03:31:08 GMT express deprecated res.json(status, obj): Use res.status(status).json(obj) instead at index.js:43:7

作為日志,我添加狀態並更改為

res.status(200).json

還是不行,請幫忙,謝謝

res.json([body])

發送JSON響應。 傳遞對象或數組時,此方法與res.send()相同。

res.json(null)
res.json({ user: 'tobi' })
res.status(500).json({ error: 'message' })

對於json API服務,您可能不需要呈現任何內容。

res.json只接受一個對象,只需刪除'host.ejs',

res.json({
   apiKey: apiKey,
   sessionId: sessionId,
   token: token
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM