无法在Heroku nodejs教程本地获取/冷却

时间:2015-11-03 17:02:59

标签: javascript node.js heroku get

以下是我的package.json文件的依赖项,其中我添加了" cool-ascii-faces。然后我需要更新我的index.js文件以获取/ cool页面,以便在每次重新加载时我会看到一个ascii面。我收到404错误,并说“无法获取/冷却”

  "dependencies": {
    "ejs": "2.3.3",
    "express": "4.13.3", 
    "cool-ascii-faces": "~1.3"

  }

下面是我的index.js文件,它调用声明很酷

var cool = require('cool-ascii-faces');
var express = require('express');
var app = express();

app.set('port', (process.env.PORT || 5000));

app.use(express.static(__dirname + '/public'));

// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

app.get('/cool', function(request, response) {
  response.render('pages/index')
});

app.get('/cool', function(request, response) {
  response.send(cool());
});

app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));

然后我运行npm install来更新依赖项,然后运行heroku本地,但是得到404错误。

任何正确方向的帮助都会很棒!

2 个答案:

答案 0 :(得分:0)

由于模块依赖性错误,启动节点Web服务器时可能会出现异常。

检查命令/终端窗口。如果您看到指向module.js文件的红色警告消息,则会出现例外情况:

$ heroku local
forego | starting web.1 on port 5000
web.1  | module.js:341

在这种情况下,您需要安装cool-ascii-faces模块。在你的节点-js-getting-started'目录,使用以下npm命令安装: $ npm i -S cool-ascii-faces

另外......您想要将索引页面路由转换回'/'。您的路由逻辑应如下所示:

app.get('/', function(request, response) {
   response.render('pages/index')
});

app.get('/cool', function(request, response) {
   response.send(cool());
});

否则,您将始终获得默认的'页面/索引'当你点击' / cool'路线而不是笑脸。

答案 1 :(得分:-1)

您不必包含

 app.set('port', (process.env.PORT || 5000));
 app.listen(app.get('port'), function() {
     console.log('Node app is running on port', app.get('port'));
 }

Heroku将在" npm start"启动服务器并动态选择端口。您不必明确指定端口。