该问题可能已经发布了大约十二次,但是我找不到解决该问题的方法。
这是我的代码:
//////////////////// VARIABLES ////////////////////
//use express
var express = require('express');
//variable to use express
var app = express();
//use the body-parser middleware to handle post data
var bodyParser = require('body-parser');
//create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false });
//create a variable for the server
//var server = require('http').Server(app)//
//////////////////// SETUP ////////////////////
//tells express to use ejs as the view/template engine
app.set('view engine', 'ejs');
//use express as middleware to serve static pages
app.use('/CSS', express.static('CSS'));
app.use('/images', express.static('images'));
//////////////////// FUNCTIONALITY ////////////////////
//sets index as the default page
app.get('/', function (req, res) {
res.render('index');
});
//serve specified pages
app.get('/:page', function (req, res) {
res.render(req.params.page);
});
app.post('/custom_rec', urlencodedParser, function (req, res) {
console.log(req.body);
res.render('custom_rec', {data: req.body});
});
const host = '0.0.0.0';
const port = process.env.PORT || 5000;
app.listen(port, host, function(){
console.log("Express server listening on port %d in %s mode", this.address().port, app.settings.env);
});
以下是相关日志:
2018-10-23T08:42:25.388218+00:00 heroku[web.1]: State changed from crashed to starting
2018-10-23T08:42:30.109913+00:00 heroku[web.1]: Starting process with command `node ./index.js`
2018-10-23T08:42:33.503841+00:00 heroku[web.1]: Process exited with status 1
2018-10-23T08:42:33.525464+00:00 heroku[web.1]: State changed from starting to crashed
2018-10-23T08:42:33.439948+00:00 app[web.1]: module.js:549
2018-10-23T08:42:33.439965+00:00 app[web.1]: throw err;
2018-10-23T08:42:33.439967+00:00 app[web.1]: ^
2018-10-23T08:42:33.439968+00:00 app[web.1]:
2018-10-23T08:42:33.439970+00:00 app[web.1]: Error: Cannot find module '/app/index.js'
2018-10-23T08:42:33.439972+00:00 app[web.1]: at Function.Module._resolveFilename (module.js:547:15)
2018-10-23T08:42:33.439973+00:00 app[web.1]: at Function.Module._load (module.js:474:25)
2018-10-23T08:42:33.439975+00:00 app[web.1]: at Function.Module.runMain (module.js:693:10)
2018-10-23T08:42:33.439976+00:00 app[web.1]: at startup (bootstrap_node.js:191:16)
2018-10-23T08:42:33.439978+00:00 app[web.1]: at bootstrap_node.js:612:3
这是我的Procfile:
web: node ./index.js
这是我的package.json:
{
"name": "anirec",
"version": "1.0.0",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "hidden"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "hidden"
},
"homepage": "hidden",
"description": "",
"dependencies": {
"body-parser": "^1.18.3",
"ejs": "^2.6.1",
"express": "^4.16.4"
},
"engines": {
"node": "8.11.4"
}
}
这是我的目录的图像:
该应用程序使用本地主机运行。
我尝试让我的服务器监听'process.env.PORT'。我尝试在我的package.json中取出“ main”。我在Procfile中尝试了相对路径。我试过重新安装node_modules。我的Procfile与index.js位于同一目录中。
我还想念什么?预先感谢。
编辑:我也尝试过重新启动服务器/ dyno。当我在终端中输入“ git ls-files”时,也会列出“ Index.js”。
答案 0 :(得分:1)
您遇到大小写敏感的问题,您的Index.js
以大写字母开头,应改为index.js
。