Node.js 为 heroku 构建 json api

Posted

技术标签:

【中文标题】Node.js 为 heroku 构建 json api【英文标题】:Node.js build json api for heroku 【发布时间】:2015-01-24 08:12:14 【问题描述】:

我想通过 node.js 构建 API 它在本地正常工作,但在云中失败。 你能帮我找到错误吗?谢谢

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

还是不行, 请帮忙,谢谢

【问题讨论】:

我删除 view(.ejs) 并只显示 json,它可以工作!但仍然不知道如何修复视图的密码。 您是否使用了正确的 opentok 语法?查看此链接https://tokbox.com/opentok/tutorials/create-session/node/看起来他们使用opentok.createSession() 函数而不是您正在使用的.generateToken() 函数。如果它在本地运行而不在云端运行,您能否确认您在云端和本地使用的是相同版本的该模块? 不行,local也是同样的问题,我只需要json回来,所以我去掉前端加载,谢谢回复 index.ejs 中有什么? 如果您尝试仅返回 JSON,您是否尝试过删除 host.ejs 条目,以便您的响应看起来像 res.json(apiKey:apiKey,sessionId:sessionId,token:token),据我了解,您应该只将对象传递给 res.json() .如果您想渲染host.ejs,请尝试单独发送res.render('host.ejs') 【参考方案1】:
res.json([body])

发送 JSON 响应。当传递对象或数组时,此方法与 res.send() 相同。

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

对于 json API 服务,您可能不需要渲染任何内容。

【讨论】:

【参考方案2】:

res.json 只接受一个对象,只需删除'host.ejs',

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

【讨论】:

以上是关于Node.js 为 heroku 构建 json api的主要内容,如果未能解决你的问题,请参考以下文章

在 Heroku 上使用 Node 读取、写入和存储 JSON

将 node.js 应用程序部署到 Heroku 时如何执行自定义构建脚本

Heroku 构建失败 - 推送被拒绝,无法编译 node.js 应用程序,无法缩小包,许多错误

端口错误:heroku 中的 vue.js+node.js 项目

部署 Node.js 应用程序时出现 Heroku 错误

Heroku 最佳实践时钟与 node.js