node.js - Express.js REST API 无法在 Heroku 上运行

标签 node.js rest express heroku

我已经编写了一个基本的node.js应用程序,它在本地使用以下命令运行:node app.js,但不在heroku中,这是我的package.json

{
  "name": "express-rest-youness",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "express": "^4.16.2",
    "express-rest": "^1.0.4"
  }
}

和我的 app.js

 var express = require('express'),
    expressRest = require('express-rest');

 var app = express();
var rest = expressRest(app);

var records = [
    {value: 'Apple'},
    {value: 'Banana'}
];

rest.get('/api/food', function(req, rest) {
    rest.ok(records);
});

rest.get('/api/food/:id', function(req, rest) {
    var record = records[req.params.id];
    if (record) rest.ok(record);
    else rest.notFound();
});

rest.put('/api/food/:id', function(req, rest) {
    records[req.params.id] = req.body;
    return rest.accepted('/api/food/' + encodeURI(req.params.id));
});

rest.post('/api/food', function(req, rest) {
    records.push(req.body);
    rest.created('/api/food/' + (records.length - 1));
});

rest.delete('/api/food/:id', function(req, rest) {
    delete records[req.params.id];
    rest.gone();
})


app.listen(8080);

当尝试访问heroku应用程序时,我得到:

应用程序错误 应用程序发生错误,无法提供您的页面。如果您是应用程序所有者,请检查您的日志以了解详细信息。

谢谢:)

最佳答案

您创建了 Procfile 吗?我建议您熟悉this tutorial .

关于node.js - Express.js REST API 无法在 Heroku 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48356799/

相关文章:

java - 如何设计支持部分模型权限的 REST 应用程序?

express - React 服务器/客户端渲染

javascript - Mongoose 模型 key 未插入 Postman 发布请求中

node.js - 如何以最小的服务器负载实时记录驾驶员的位置和在线状态?

javascript - 错误 : write EPROTO 34557064:error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER

java - 当端点非常相似时,路径参数与查询参数

node.js - Socket.io 在 Express Route 上发出

javascript - NodeJS : Searching for specific string with fs. readFile() 多个文件

node.js - 如何显示 DynamoDB 表的内容

java - java.lang.UnsupportedOperationException 的 HTTP 501