【问题标题】:Cannot GET /cool on Heroku nodejs tutorial locally无法在本地获取 Heroku nodejs 教程上的 /cool
【发布时间】:2016-02-03 22:51:58
【问题描述】:

这是我的 package.json 文件的依赖项,我在其中添加了“cool-ascii-faces。然后我需要更新我的 index.js 文件以获取 /cool 页面,以便在每次重新加载时我都会看到一个ascii 脸。我收到 404 错误,上面写着 'Cannot GET /cool'

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

  }

下面是我的 index.js 文件,它调用了 declares cool

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 local,但得到 404 错误。

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

【问题讨论】:

    标签: javascript node.js heroku get


    【解决方案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”来启动您的服务器并动态选择端口。您不必明确指定端口。

    【讨论】:

      【解决方案2】:

      由于模块依赖错误,您可能会在启动节点 Web 服务器时遇到异常。

      检查您的命令/终端窗口。如果您看到指向您的 module.js 文件的红色警告消息,则说明您遇到了异常:

      $ heroku 本地 放弃|在端口 5000 上启动 web.1 web.1 |模块.js:341

      在这种情况下,您需要安装 cool-ascii-faces 模块。在您的“node-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”路线时,您将始终获得默认的“pages/index”页面,而不是笑脸。

      【讨论】:

        【解决方案3】:

        这个问题发生在我身上。在我输入“git push heroku master”之后,我输入了“heroku open”,它就起作用了。

        【讨论】:

          猜你喜欢
          • 2022-09-30
          • 1970-01-01
          • 2020-05-25
          • 1970-01-01
          • 2017-07-12
          • 1970-01-01
          • 1970-01-01
          • 2015-06-11
          • 2017-08-30
          相关资源
          最近更新 更多