0

所以查看我的日志,我看到了:

2016-10-24T05:42:16.071986+00:00 app[web.1]: var app = express();
2016-10-24T05:42:16.071987+00:00 app[web.1]:           ^
2016-10-24T05:42:16.071987+00:00 app[web.1]: 
2016-10-24T05:42:16.071988+00:00 app[web.1]: TypeError: express is not a function
2016-10-24T05:42:16.071989+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:2:11)
2016-10-24T05:42:16.071989+00:00 app[web.1]:     at Module._compile (module.js:413:34)
2016-10-24T05:42:16.071990+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:422:10)
2016-10-24T05:42:16.071990+00:00 app[web.1]:     at Module.load (module.js:357:32)
2016-10-24T05:42:16.071990+00:00 app[web.1]:     at Function.Module._load (module.js:314:12)
2016-10-24T05:42:16.071991+00:00 app[web.1]:     at Function.Module.runMain (module.js:447:10)
2016-10-24T05:42:16.071991+00:00 app[web.1]:     at startup (node.js:148:18)
2016-10-24T05:42:16.071995+00:00 app[web.1]:     at node.js:405:3
2016-10-24T05:42:16.162284+00:00 heroku[web.1]: State changed from starting to crashed
2016-10-24T05:42:25.379955+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/local" host=murmuring-reef-69157.herokuapp.com request_id=3c2c9cc9-d1b2-48b1-b628-bd5d8030fd0e fwd="98.210.167.212" dyno= connect= service= status=503 bytes=
2016-10-24T05:42:26.006599+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=murmuring-reef-69157.herokuapp.com request_id=f4507e17-8202-46cf-ad81-4737ccd1bbab fwd="98.210.167.212" dyno= connect= service= status=503 bytes=
2016-10-24T05:48:10.261072+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=murmuring-reef-69157.herokuapp.com request_id=f3d0a1b2-4d0c-4ad7-af35-d18e0b3e6e72 fwd="98.210.167.212" dyno= connect= service= status=503 bytes=
2016-10-24T05:48:10.866238+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=murmuring-reef-69157.herokuapp.com request_id=12526a0f-14c2-4ecf-88c7-a656000c9d6d fwd="98.210.167.212" dyno= connect= service= status=503 bytes=
2016-10-24T05:49:48.551924+00:00 heroku[web.1]: State changed from crashed to starting
2016-10-24T05:49:51.371049+00:00 heroku[web.1]: Starting process with command `node app.js`
2016-10-24T05:49:53.339799+00:00 app[web.1]: /app/app.js:2
2016-10-24T05:49:53.339818+00:00 app[web.1]: var app = express();
2016-10-24T05:49:53.339819+00:00 app[web.1]:           ^
2016-10-24T05:49:53.339819+00:00 app[web.1]: 
2016-10-24T05:49:53.339821+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:2:11)
2016-10-24T05:49:53.339822+00:00 app[web.1]:     at Module._compile (module.js:413:34)
2016-10-24T05:49:53.339820+00:00 app[web.1]: TypeError: express is not a function
2016-10-24T05:49:53.339823+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:422:10)
2016-10-24T05:49:53.339823+00:00 app[web.1]:     at Module.load (module.js:357:32)
2016-10-24T05:49:53.339824+00:00 app[web.1]:     at Function.Module._load (module.js:314:12)
2016-10-24T05:49:53.339824+00:00 app[web.1]:     at Function.Module.runMain (module.js:447:10)
2016-10-24T05:49:53.339825+00:00 app[web.1]:     at startup (node.js:148:18)
2016-10-24T05:49:53.339825+00:00 app[web.1]:     at node.js:405:3
2016-10-24T05:49:53.462911+00:00 heroku[web.1]: Process exited with status 1
2016-10-24T05:49:53.481857+00:00 heroku[web.1]: State changed from starting to crashed
2016-10-24T05:51:59.092552+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=murmuring-reef-69157.herokuapp.com request_id=83f531a2-2855-4c25-bd31-21293e939156 fwd="98.210.167.212" dyno= connect= service= status=503 bytes=
2016-10-24T05:51:59.702922+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=murmuring-reef-69157.herokuapp.com request_id=a4d24924-75a4-4440-8f14-a914ea1441f4 fwd="98.210.167.212" dyno= connect= service= status=503 bytes=
2016-10-24T05:55:27.129395+00:00 heroku[api]: Deploy 383a228 by kenzom954@gmail.com
2016-10-24T05:55:27.129485+00:00 heroku[api]: Release v9 created by kenzom954@gmail.com
2016-10-24T05:55:27.308319+00:00 heroku[slug-compiler]: Slug compilation started
2016-10-24T05:55:27.308324+00:00 heroku[slug-compiler]: Slug compilation finished
2016-10-24T05:55:27.289407+00:00 heroku[web.1]: State changed from crashed to starting
2016-10-24T05:55:30.183810+00:00 heroku[web.1]: Starting process with command `node app.js`
2016-10-24T05:55:32.094514+00:00 app[web.1]: var app = express();
2016-10-24T05:55:32.094515+00:00 app[web.1]:           ^
2016-10-24T05:55:32.094499+00:00 app[web.1]: /app/app.js:2
2016-10-24T05:55:32.094516+00:00 app[web.1]: TypeError: express is not a function
2016-10-24T05:55:32.094516+00:00 app[web.1]: 
2016-10-24T05:55:32.094517+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:2:11)
2016-10-24T05:55:32.094518+00:00 app[web.1]:     at Module._compile (module.js:413:34)
2016-10-24T05:55:32.094519+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:422:10)
2016-10-24T05:55:32.094520+00:00 app[web.1]:     at Module.load (module.js:357:32)
2016-10-24T05:55:32.094521+00:00 app[web.1]:     at Function.Module.runMain (module.js:447:10)
2016-10-24T05:55:32.094520+00:00 app[web.1]:     at Function.Module._load (module.js:314:12)
2016-10-24T05:55:32.094522+00:00 app[web.1]:     at startup (node.js:148:18)
2016-10-24T05:55:32.094522+00:00 app[web.1]:     at node.js:405:3
2016-10-24T05:55:32.206636+00:00 heroku[web.1]: State changed from starting to crashed
2016-10-24T05:55:32.190134+00:00 heroku[web.1]: Process exited with status 1
2016-10-24T05:58:55.619526+00:00 heroku[api]: Deploy 211f725 by kenzom954@gmail.com
2016-10-24T05:58:55.619564+00:00 heroku[api]: Release v10 created by kenzom954@gmail.com
2016-10-24T05:58:55.819150+00:00 heroku[slug-compiler]: Slug compilation started
2016-10-24T05:58:55.819157+00:00 heroku[slug-compiler]: Slug compilation finished
2016-10-24T05:58:56.098775+00:00 heroku[web.1]: State changed from crashed to starting
2016-10-24T05:58:59.294388+00:00 heroku[web.1]: Starting process with command `node app.js`
2016-10-24T05:59:01.290759+00:00 app[web.1]: module.js:341
2016-10-24T05:59:01.290772+00:00 app[web.1]:     throw err;
2016-10-24T05:59:01.290773+00:00 app[web.1]:     ^
2016-10-24T05:59:01.290774+00:00 app[web.1]: 
2016-10-24T05:59:01.290775+00:00 app[web.1]: Error: Cannot find module 'body-parser'
2016-10-24T05:59:01.290776+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:339:15)
2016-10-24T05:59:01.290776+00:00 app[web.1]:     at Function.Module._load (module.js:290:25)
2016-10-24T05:59:01.290777+00:00 app[web.1]:     at Module.require (module.js:367:17)
2016-10-24T05:59:01.290778+00:00 app[web.1]:     at require (internal/module.js:20:19)
2016-10-24T05:59:01.290778+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:3:18)
2016-10-24T05:59:01.290779+00:00 app[web.1]:     at Module._compile (module.js:413:34)
2016-10-24T05:59:01.290780+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:422:10)
2016-10-24T05:59:01.290780+00:00 app[web.1]:     at Module.load (module.js:357:32)
2016-10-24T05:59:01.290781+00:00 app[web.1]:     at Function.Module._load (module.js:314:12)
2016-10-24T05:59:01.290781+00:00 app[web.1]:     at Function.Module.runMain (module.js:447:10)
2016-10-24T05:59:01.369506+00:00 heroku[web.1]: State changed from starting to crashed
2016-10-24T05:59:01.356300+00:00 heroku[web.1]: Process exited with status 1
2016-10-24T05:59:03.922494+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=murmuring-reef-69157.herokuapp.com request_id=167632f8-7e55-4961-9a96-c630a5bfd05c fwd="98.210.167.212" dyno= connect= service= status=503 bytes=
2016-10-24T05:59:04.559487+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=murmuring-reef-69157.herokuapp.com request_id=2facb77e-53f1-4f95-b150-7f7e05b9622e fwd="98.210.167.212" dyno= connect= service= status=503 bytes=
2016-10-24T06:01:06.014637+00:00 heroku[web.1]: State changed from crashed to starting
2016-10-24T06:01:09.793263+00:00 heroku[web.1]: Starting process with command `node app.js`
2016-10-24T06:01:11.927280+00:00 app[web.1]: module.js:341
2016-10-24T06:01:11.927293+00:00 app[web.1]:     throw err;
2016-10-24T06:01:11.927294+00:00 app[web.1]: 
2016-10-24T06:01:11.927294+00:00 app[web.1]:     ^
2016-10-24T06:01:11.927295+00:00 app[web.1]: Error: Cannot find module 'body-parser'
2016-10-24T06:01:11.927296+00:00 app[web.1]:     at Function.Module._resolveFilename (module.js:339:15)
2016-10-24T06:01:11.927296+00:00 app[web.1]:     at Function.Module._load (module.js:290:25)
2016-10-24T06:01:11.927297+00:00 app[web.1]:     at Module.require (module.js:367:17)
2016-10-24T06:01:11.927297+00:00 app[web.1]:     at require (internal/module.js:20:19)
2016-10-24T06:01:11.927298+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:3:18)
2016-10-24T06:01:11.927299+00:00 app[web.1]:     at Module._compile (module.js:413:34)
2016-10-24T06:01:11.927299+00:00 app[web.1]:     at Object.Module._extensions..js (module.js:422:10)
2016-10-24T06:01:11.927300+00:00 app[web.1]:     at Module.load (module.js:357:32)
2016-10-24T06:01:11.927300+00:00 app[web.1]:     at Function.Module._load (module.js:314:12)
2016-10-24T06:01:11.927301+00:00 app[web.1]:     at Function.Module.runMain (module.js:447:10)
2016-10-24T06:01:12.028090+00:00 heroku[web.1]: Process exited with status 1
2016-10-24T06:01:12.045170+00:00 heroku[web.1]: State changed from starting to crashed

但是,在npm install送到.git push heroku master

当我在本地运行时,heroku local它可以正常工作,但heroku open会导致我犯那些错误。

应用程序.js

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var port = process.env.PORT ||  3000;
var crypto = require("crypto");
var path = require('path');
var config = require('./config'); // holds information about hosting
var Url = require('./models/Url.model'); //this holds our Schema model
var LookUp = require('./models/Lookup.model.js'); //this holds our Schema lookup
var db ='mongodb://heroku_0xrn515v:v09cpj5t25qtm73klv2poj5n5h@ds031157.mlab.com:31157/heroku_0xrn515v'

mongoose.connect(db)
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/',function(req,res){
  res.sendFile(path.join(__dirname, 'views/index.html'));
})

app.post('/api/shorten',function(req,res){
  var regex = /https:\/\/|http:\/\//
    //Check if the user input correctly with http or https format
    //if true, proceed. Otherwise, return a string with home direct link
  if(regex.test(req.body.long_url)){
    var longUrl = req.body.long_url;
    var shortUrl = ""
    //check if document (url) exist already in collection
    Url.findOne({
      long_url : longUrl
    })
      .exec(function(err,result){
        if (err) throw err
        else{
          //if the long url is in the collection, simply return the short_url
          if(result){
            LookUp.findOne({
              key: result._id
            })
              .exec(function(err,result){
                res.send({shortUrl: config.webhost + result.shortUrl,
                  shortUrlString: config.webhost + result.shortUrl })
              })
          }
          //if the long url isn't in the collection, make a new short_url for it
          else{
            var newUrl = new Url();
            newUrl.long_url = longUrl
            newUrl.save( function(err,result){
              if(err){
                console.log('error!')
              } else{
                var newLookUp = new LookUp();
                newLookUp.key = result._id;
                var id = crypto.randomBytes(2).toString('hex');
                newLookUp.shortUrl = id
                newLookUp.save( function( err, result){
                  if (err) throw err
                  else{
                    res.send({shortUrl: config.webhost + result.shortUrl,
                    shortUrlString: config.webhost + result.shortUrl})
                  }
                })
              }
            })
          }
        }
      })
  } else{
    res.send({shortUrl: "#"
      ,shortUrlString: 'Remember to put in https:// format!'})
  }
})

app.get('/:id', function(req, res){
  var shortenURL = req.params.id;
  LookUp.findOne({
    shortUrl: shortenURL
  })
    .exec(function(err,result){
      Url.findOne({
        _id: result.key
      })
        .exec(function(err,resultLink){
          res.redirect(resultLink.long_url)
        })
    })
});

app.listen(port,function(){
  console.log('listening in port', port)
})

包.json

{
  "name": "shortenerurl",
  "version": "1.0.0",
  "description": "",
  "main": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "crypto": "0.0.3",
    "express": "^4.14.0",
    "mongoose": "^4.6.5"
  }
}

档案

web: node app.js

我错过了什么?

4

3 回答 3

1

Heroku 使用的 Node 版本可能有问题。您可以尝试将 Heroku 设置为使用您在本地使用的同一个。此外,您不需要npm install在部署到 Heroku 之前进行操作,因为它会在您部署时为您服务(因此不要在 git 上包含 node_module 文件夹)。

于 2016-10-24T13:13:14.430 回答
0

就我而言,问题出在环境变量中。在我的本地系统中,我有 .env 文件,Environment variables但我忘记在 heroku 上设置环境变量,这给了我错误。您可以使用设置 ENV 变量

$ heroku config:set ENV_KEY="the_value"
于 2019-09-27T08:48:39.933 回答
0

我遇到了这个错误,经过大量搜索,@hammad 的答案对我有用,以及如何通过两种方式设置配置变量

  1. 使用heroku CLI。
  2. 使用仪表板(我很容易遵循)。仪表板>您的应用程序>设置>配置变量>设置键=值就是这样。

https://devcenter.heroku.com/articles/config-vars

于 2021-06-17T14:33:26.853 回答