我已经编写了一个基本的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/