我正在尝试部署我用Express和Node编写的API。我已尝试通过连接到Github和使用Heroku Toolbelt来部署它。按照日志中的说法,应用程序已成功部署,但我转到页面并始终says:
应用程序错误
应用程序中发生错误,无法提供您的页面。请稍后再试。
如果您是应用程序所有者,请查看日志以获取详细信息。
我用web: node index.js
以下是package.json
和index.js
个文件中的代码:
package.json
:
{
"name": "timestamp-microservice",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "edward-hong",
"repository": {
"type": "git",
"url": "https://github.com/edward-hong/timestamp-microservice.git"
},
"license": "ISC",
"engines": {
"node": "4.4.0"
},
"scripts": {
"start": "node index.js"
},
"dependencies": {
"body-parser": "^1.15.1",
"express": "^4.13.4",
"moment": "^2.13.0"
}
}
index.js
:
var express = require('express');
var bodyParser = require('body-parser');
var moment = require('moment');
var port = process.env.PORT || 3000;
var app = express();
var timeRouter = express.Router();
timeRouter.use(bodyParser.json());
timeRouter.route('/:time')
.get(function(req, res, next){
var time = req.params.time;
var unixTime = Number(time);
if (unixTime){
res.json({'unix': unixTime, 'natural': moment.unix(unixTime).format('MMMM D, YYYY')});
} else {
if (moment(time).isValid()){
res.json({'unix': Number(moment(time).format('X')), 'natural': moment(time).format('MMMM D, YYYY')});
} else {
res.json({'unix': null, 'natural': null});
}
}
});
app.use('/', timeRouter);
app.use(express.static(__dirname, 'index.html'));
app.listen(port, function(){
console.log('Server running at http://oursite:' + port);
});
以下是日志:
2016-05-14T09:28:27.238579+00:00 heroku[web.1]: Process exited with status 1
2016-05-14T09:28:47.111767+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=eh-timestamp-microservice.herokuapp.com request_id=4fe2d06a-8476-4e70-9b0d-c88c68f9f6b1 fwd="163.47.236.3" dyno= connect= service= status=503 bytes=
2016-05-14T09:28:48.422607+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=eh-timestamp-microservice.herokuapp.com request_id=80b36992-82a7-4938-8a2e-4e0abec86663 fwd="163.47.236.3" dyno= connect= service= status=503 bytes=
2016-05-14T09:39:28.089591+00:00 heroku[web.1]: State changed from crashed to starting
2016-05-14T09:39:29.564906+00:00 heroku[web.1]: Starting process with command `node index.js`
2016-05-14T09:39:32.277699+00:00 app[web.1]: /app/node_modules/express/node_modules/serve-static/index.js:47
2016-05-14T09:39:32.277742+00:00 app[web.1]: var opts = Object.create(options || null)
2016-05-14T09:39:32.277760+00:00 app[web.1]: ^
2016-05-14T09:39:32.277770+00:00 app[web.1]:
2016-05-14T09:39:32.277795+00:00 app[web.1]: TypeError: Object prototype may only be an Object or null: index.html
2016-05-14T09:39:32.277798+00:00 app[web.1]: at Function.serveStatic [as static] (/app/node_modules/express/node_modules/serve-static/index.js:47:21)
2016-05-14T09:39:32.277797+00:00 app[web.1]: at Function.create (native)
2016-05-14T09:39:32.277798+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:23:23)
2016-05-14T09:39:32.277799+00:00 app[web.1]: at Module._compile (module.js:409:26)
2016-05-14T09:39:32.277802+00:00 app[web.1]: at startup (node.js:139:18)
2016-05-14T09:39:32.277812+00:00 app[web.1]: at node.js:968:3
2016-05-14T09:39:32.277800+00:00 app[web.1]: at Module.load (module.js:343:32)
2016-05-14T09:39:32.277799+00:00 app[web.1]: at Object.Module._extensions..js (module.js:416:10)
2016-05-14T09:39:32.277800+00:00 app[web.1]: at Function.Module._load (module.js:300:12)
2016-05-14T09:39:32.277801+00:00 app[web.1]: at Function.Module.runMain (module.js:441:10)
2016-05-14T09:39:33.321097+00:00 heroku[web.1]: Process exited with status 1
2016-05-14T09:39:33.343282+00:00 heroku[web.1]: State changed from starting to crashed
2016-05-14T09:40:18.797349+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/robots.txt" host=eh-timestamp-microservice.herokuapp.com request_id=a351f860-7b5c-4ce7-a3b3-65fbb419b44f fwd="91.121.64.34" dyno= connect= service= status=503 bytes=
2016-05-14T09:40:19.725084+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=eh-timestamp-microservice.herokuapp.com request_id=9627bc3a-51e0-470d-8a19-60dfda8df6b2 fwd="51.255.122.75" dyno= connect= service= status=503 bytes=
2016-05-14T09:43:03.346097+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=eh-timestamp-microservice.herokuapp.com request_id=da725ac8-1aa6-4801-b33c-875395c97f8f fwd="122.171.196.55" dyno= connect= service= status=503 bytes=
2016-05-14T09:43:05.023995+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=eh-timestamp-microservice.herokuapp.com request_id=3e13f96b-4605-4b12-891f-d2add65e32a6 fwd="122.171.196.55" dyno= connect= service= status=503 bytes=
2016-05-14T09:43:55.572621+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/?123" host=eh-timestamp-microservice.herokuapp.com request_id=e4142733-b39e-44da-964d-b2507c7fdfd6 fwd="122.171.196.55" dyno= connect= service= status=503 bytes=
2016-05-14T09:43:55.958412+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=eh-timestamp-microservice.herokuapp.com request_id=597b272e-dd2b-4646-a099-1ca009bb49c7 fwd="122.171.196.55" dyno= connect= service= status=503 bytes=
2016-05-14T09:43:57.833280+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/123" host=eh-timestamp-microservice.herokuapp.com request_id=4a33ecea-5813-4c8d-9a21-a1a089170120 fwd="122.171.196.55" dyno= connect= service= status=503 bytes=
2016-05-14T09:43:58.247554+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=eh-timestamp-microservice.herokuapp.com request_id=1561f774-9d38-4003-b37e-29392bc962ac fwd="122.171.196.55" dyno= connect= service= status=503 bytes=
2016-05-14T09:49:27.982002+00:00 heroku[api]: Release v10 created by edward.hong527@gmail.com
2016-05-14T09:49:27.982002+00:00 heroku[api]: Deploy 182e72e by edward.hong527@gmail.com
2016-05-14T09:49:28.148769+00:00 heroku[slug-compiler]: Slug compilation started
2016-05-14T09:49:28.148779+00:00 heroku[slug-compiler]: Slug compilation finished
2016-05-14T09:49:28.171518+00:00 heroku[web.1]: State changed from crashed to starting
2016-05-14T09:49:29.761171+00:00 heroku[web.1]: Starting process with command `node index.js`
2016-05-14T09:49:31.808646+00:00 app[web.1]: /app/node_modules/express/node_modules/serve-static/index.js:47
2016-05-14T09:49:31.808665+00:00 app[web.1]: var opts = Object.create(options || null)
2016-05-14T09:49:31.808666+00:00 app[web.1]: ^
2016-05-14T09:49:31.808668+00:00 app[web.1]: TypeError: Object prototype may only be an Object or null: index.html
2016-05-14T09:49:31.808667+00:00 app[web.1]:
2016-05-14T09:49:31.808669+00:00 app[web.1]: at Function.create (native)
2016-05-14T09:49:31.808669+00:00 app[web.1]: at Function.serveStatic [as static] (/app/node_modules/express/node_modules/serve-static/index.js:47:21)
2016-05-14T09:49:31.808670+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:23:23)
2016-05-14T09:49:31.808670+00:00 app[web.1]: at Module._compile (module.js:409:26)
2016-05-14T09:49:31.808671+00:00 app[web.1]: at Object.Module._extensions..js (module.js:416:10)
2016-05-14T09:49:31.808672+00:00 app[web.1]: at Module.load (module.js:343:32)
2016-05-14T09:49:31.808672+00:00 app[web.1]: at Function.Module._load (module.js:300:12)
2016-05-14T09:49:31.808673+00:00 app[web.1]: at Function.Module.runMain (module.js:441:10)
2016-05-14T09:49:31.808673+00:00 app[web.1]: at startup (node.js:139:18)
2016-05-14T09:49:31.808674+00:00 app[web.1]: at node.js:968:3
2016-05-14T09:49:32.501619+00:00 heroku[web.1]: Process exited with status 1
2016-05-14T09:49:32.536377+00:00 heroku[web.1]: State changed from starting to crashed
2016-05-14T09:49:38.856013+00:00 heroku[slug-compiler]: Slug compilation started
2016-05-14T09:49:38.856024+00:00 heroku[slug-compiler]: Slug compilation finished
2016-05-14T09:49:38.704760+00:00 heroku[api]: Deploy 182e72e by edward.hong527@gmail.com
2016-05-14T09:49:38.704788+00:00 heroku[api]: Release v11 created by edward.hong527@gmail.com
2016-05-14T09:49:38.918627+00:00 heroku[web.1]: State changed from crashed to starting
2016-05-14T09:49:39.993466+00:00 heroku[web.1]: Starting process with command `node index.js`
2016-05-14T09:49:42.145320+00:00 app[web.1]: /app/node_modules/express/node_modules/serve-static/index.js:47
2016-05-14T09:49:42.145342+00:00 app[web.1]: var opts = Object.create(options || null)
2016-05-14T09:49:42.145344+00:00 app[web.1]: ^
2016-05-14T09:49:42.145345+00:00 app[web.1]:
2016-05-14T09:49:42.145346+00:00 app[web.1]: TypeError: Object prototype may only be an Object or null: index.html
2016-05-14T09:49:42.145346+00:00 app[web.1]: at Function.create (native)
2016-05-14T09:49:42.145347+00:00 app[web.1]: at Function.serveStatic [as static] (/app/node_modules/express/node_modules/serve-static/index.js:47:21)
2016-05-14T09:49:42.145348+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:23:23)
2016-05-14T09:49:42.145348+00:00 app[web.1]: at Module._compile (module.js:409:26)
2016-05-14T09:49:42.145349+00:00 app[web.1]: at Object.Module._extensions..js (module.js:416:10)
2016-05-14T09:49:42.145350+00:00 app[web.1]: at Module.load (module.js:343:32)
2016-05-14T09:49:42.145350+00:00 app[web.1]: at Function.Module._load (module.js:300:12)
2016-05-14T09:49:42.145351+00:00 app[web.1]: at Function.Module.runMain (module.js:441:10)
2016-05-14T09:49:42.145352+00:00 app[web.1]: at startup (node.js:139:18)
2016-05-14T09:49:42.145352+00:00 app[web.1]: at node.js:968:3
2016-05-14T09:49:42.929388+00:00 heroku[web.1]: State changed from starting to crashed
2016-05-14T09:49:42.909282+00:00 heroku[web.1]: Process exited with status 1
2016-05-14T09:50:05.626329+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=eh-timestamp-microservice.herokuapp.com request_id=cdee94cc-a0bf-4907-b442-1e72a09d1e20 fwd="163.47.236.3" dyno= connect= service= status=503 bytes=
2016-05-14T09:50:07.477549+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=eh-timestamp-microservice.herokuapp.com request_id=88cbbbca-5afc-4740-a934-751718040503 fwd="163.47.236.3" dyno= connect= service= status=503 bytes=
2016-05-14T09:55:36.623731+00:00 heroku[web.1]: State changed from crashed to starting
2016-05-14T09:55:37.903604+00:00 heroku[web.1]: Starting process with command `node index.js`
2016-05-14T09:55:39.780790+00:00 app[web.1]: /app/node_modules/express/node_modules/serve-static/index.js:47
2016-05-14T09:55:39.780809+00:00 app[web.1]: var opts = Object.create(options || null)
2016-05-14T09:55:39.780810+00:00 app[web.1]: ^
2016-05-14T09:55:39.780810+00:00 app[web.1]:
2016-05-14T09:55:39.780811+00:00 app[web.1]: TypeError: Object prototype may only be an Object or null: index.html
2016-05-14T09:55:39.780811+00:00 app[web.1]: at Function.create (native)
2016-05-14T09:55:39.780812+00:00 app[web.1]: at Function.serveStatic [as static] (/app/node_modules/express/node_modules/serve-static/index.js:47:21)
2016-05-14T09:55:39.780812+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:23:23)
2016-05-14T09:55:39.780813+00:00 app[web.1]: at Module._compile (module.js:409:26)
2016-05-14T09:55:39.780813+00:00 app[web.1]: at Object.Module._extensions..js (module.js:416:10)
2016-05-14T09:55:39.780813+00:00 app[web.1]: at Module.load (module.js:343:32)
2016-05-14T09:55:39.780814+00:00 app[web.1]: at Function.Module._load (module.js:300:12)
2016-05-14T09:55:39.780814+00:00 app[web.1]: at Function.Module.runMain (module.js:441:10)
2016-05-14T09:55:39.780814+00:00 app[web.1]: at startup (node.js:139:18)
2016-05-14T09:55:39.780815+00:00 app[web.1]: at node.js:968:3
2016-05-14T09:55:40.461214+00:00 heroku[web.1]: State changed from starting to crashed
2016-05-14T09:55:40.440635+00:00 heroku[web.1]: Process exited with status 1
2016-05-14T09:59:58.706941+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=eh-timestamp-microservice.herokuapp.com request_id=9fe4e275-f9e0-49df-bc6f-a90395578d35 fwd="163.47.236.3" dyno= connect= service= status=503 bytes=
2016-05-14T09:59:59.785150+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=eh-timestamp-microservice.herokuapp.com request_id=59101524-ec76-4742-9b6f-77003bf0ab0f fwd="163.47.236.3" dyno= connect= service= status=503 bytes=
2016-05-14T10:00:18.450815+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=eh-timestamp-microservice.herokuapp.com request_id=42c2b2aa-a549-48db-bebd-734f910b2f94 fwd="42.236.50.154" dyno= connect= service= status=503 bytes=
2016-05-14T10:07:05.926172+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/robots.txt" host=eh-timestamp-microservice.herokuapp.com request_id=05d5ab32-ba20-428f-9886-4baae5456bc9 fwd="182.118.35.23" dyno= connect= service= status=503 bytes=
我想知道如何将它成功部署到Heroku上?
答案 0 :(得分:2)
静态中间件有两个参数,一个字符串,它是要服务的目录的根路径,以及一个选项对象。
例如
serveStatic('public/ftp', {'index': ['default.html', 'default.htm']})
在你的情况下,第二个参数是一个字符串,这是不正确的。