首页 > 解决方案 > Heroku 错误:无法在视图目录“/views”中查找视图“home”

问题描述

当我在heroku中部署我的node.js网页时,我没有在日志中收到任何错误,但是当我转到生成的 url 时,我收到了这个错误:

2021-07-28T18:19:34.097528+00:00 app[web.1]: Example app listening on port 17754!
2021-07-28T18:19:34.507324+00:00 heroku[web.1]: State changed from starting to up
2021-07-28T18:19:35.830833+00:00 heroku[router]: at=info method=GET path="/" host=ogscs.herokuapp.com request_id=17adbc39-aa52-41b4-a505-ec5455b6409b fwd="200.58.175.148" dyno=web.1 connect=1ms service=175ms status=500 bytes=567 protocol=https
2021-07-28T18:19:35.826148+00:00 app[web.1]: Error: Failed to lookup view "home" in views directory "app/views"
2021-07-28T18:19:35.826156+00:00 app[web.1]:     at Function.render (/app/node_modules/express/lib/application.js:580:17)
2021-07-28T18:19:35.826157+00:00 app[web.1]:     at ServerResponse.render (/app/node_modules/express/lib/response.js:1012:7)
2021-07-28T18:19:35.826157+00:00 app[web.1]:     at /app/app.js:86:6
2021-07-28T18:19:35.826158+00:00 app[web.1]:     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2021-07-28T18:19:35.826158+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/route.js:137:13)
2021-07-28T18:19:35.826159+00:00 app[web.1]:     at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
2021-07-28T18:19:35.826163+00:00 app[web.1]:     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2021-07-28T18:19:35.826163+00:00 app[web.1]:     at /app/node_modules/express/lib/router/index.js:281:22
2021-07-28T18:19:35.826164+00:00 app[web.1]:     at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)
2021-07-28T18:19:35.826164+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/index.js:275:10)
2021-07-28T18:19:35.826165+00:00 app[web.1]:     at /app/app.js:66:2
2021-07-28T18:19:35.826165+00:00 app[web.1]:     at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
2021-07-28T18:19:35.826165+00:00 app[web.1]:     at trim_prefix (/app/node_modules/express/lib/router/index.js:317:13)
2021-07-28T18:19:35.826166+00:00 app[web.1]:     at /app/node_modules/express/lib/router/index.js:284:7
2021-07-28T18:19:35.826166+00:00 app[web.1]:     at Function.process_params (/app/node_modules/express/lib/router/index.js:335:12)
2021-07-28T18:19:35.826166+00:00 app[web.1]:     at next (/app/node_modules/express/lib/router/index.js:275:10)
2021-07-28T18:20:03.978551+00:00 app[web.1]: connection error: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
2021-07-28T18:20:03.978564+00:00 app[web.1]:     at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:846:32)
2021-07-28T18:20:03.978565+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/index.js:351:10
2021-07-28T18:20:03.978566+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
2021-07-28T18:20:03.978567+00:00 app[web.1]:     at new Promise (<anonymous>)
2021-07-28T18:20:03.978567+00:00 app[web.1]:     at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
2021-07-28T18:20:03.978567+00:00 app[web.1]:     at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1149:10)
2021-07-28T18:20:03.978568+00:00 app[web.1]:     at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:350:20)
2021-07-28T18:20:03.978568+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:8:10)
2021-07-28T18:20:03.978569+00:00 app[web.1]:     at Module._compile (internal/modules/cjs/loader.js:1085:14)
2021-07-28T18:20:03.978569+00:00 app[web.1]:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
2021-07-28T18:20:03.978569+00:00 app[web.1]:     at Module.load (internal/modules/cjs/loader.js:950:32)
2021-07-28T18:20:03.978570+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:790:14)
2021-07-28T18:20:03.978570+00:00 app[web.1]:     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
2021-07-28T18:20:03.978571+00:00 app[web.1]:     at internal/main/run_main_module.js:17:47 {
2021-07-28T18:20:03.978571+00:00 app[web.1]:   reason: TopologyDescription {
2021-07-28T18:20:03.978571+00:00 app[web.1]:     type: 'Single',
2021-07-28T18:20:03.978572+00:00 app[web.1]:     setName: null,
2021-07-28T18:20:03.978572+00:00 app[web.1]:     maxSetVersion: null,
2021-07-28T18:20:03.978573+00:00 app[web.1]:     maxElectionId: null,
2021-07-28T18:20:03.978573+00:00 app[web.1]:     servers: Map(1) { 'localhost:27017' => [ServerDescription] },
2021-07-28T18:20:03.978573+00:00 app[web.1]:     stale: false,
2021-07-28T18:20:03.978574+00:00 app[web.1]:     compatible: true,
2021-07-28T18:20:03.978574+00:00 app[web.1]:     compatibilityError: null,
2021-07-28T18:20:03.978575+00:00 app[web.1]:     logicalSessionTimeoutMinutes: null,
2021-07-28T18:20:03.978575+00:00 app[web.1]:     heartbeatFrequencyMS: 10000,
2021-07-28T18:20:03.978575+00:00 app[web.1]:     localThresholdMS: 15,
2021-07-28T18:20:03.978576+00:00 app[web.1]:     commonWireVersion: null
2021-07-28T18:20:03.978576+00:00 app[web.1]:   }
2021-07-28T18:20:03.978577+00:00 app[web.1]: }
2021-07-28T18:20:03.980443+00:00 app[web.1]: (node:21) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
2021-07-28T18:20:03.980444+00:00 app[web.1]:     at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:846:32)
2021-07-28T18:20:03.980445+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/index.js:351:10
2021-07-28T18:20:03.980446+00:00 app[web.1]:     at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:32:5
2021-07-28T18:20:03.980448+00:00 app[web.1]:     at new Promise (<anonymous>)
2021-07-28T18:20:03.980448+00:00 app[web.1]:     at promiseOrCallback (/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:10)
2021-07-28T18:20:03.980449+00:00 app[web.1]:     at Mongoose._promiseOrCallback (/app/node_modules/mongoose/lib/index.js:1149:10)
2021-07-28T18:20:03.980449+00:00 app[web.1]:     at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:350:20)
2021-07-28T18:20:03.980449+00:00 app[web.1]:     at Object.<anonymous> (/app/app.js:8:10)
2021-07-28T18:20:03.980450+00:00 app[web.1]:     at Module._compile (internal/modules/cjs/loader.js:1085:14)
2021-07-28T18:20:03.980450+00:00 app[web.1]:     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
2021-07-28T18:20:03.980451+00:00 app[web.1]:     at Module.load (internal/modules/cjs/loader.js:950:32)
2021-07-28T18:20:03.980451+00:00 app[web.1]:     at Function.Module._load (internal/modules/cjs/loader.js:790:14)
2021-07-28T18:20:03.980452+00:00 app[web.1]:     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
2021-07-28T18:20:03.980452+00:00 app[web.1]:     at internal/main/run_main_module.js:17:47
2021-07-28T18:20:03.980452+00:00 app[web.1]: (Use `node --trace-warnings ...` to show where the warning was created)
2021-07-28T18:20:03.984519+00:00 app[web.1]: (node:21) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
2021-07-28T18:20:03.984858+00:00 app[web.1]: (node:21) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Ingore mongoose 错误,我已经解决了这个问题,但不断收到这个错误 Error: Failed to lookup view "home" in views directory "app/views"我的 node-express 视图和公共路径是:

app.use(express.static(path.join(__dirname, '/public')));
app.set('views', path.join(__dirname, '/views'));
//my port is on
const port = process.env.PORT || 3000

我试过这个:

app.use(express.static(path.join(__dirname, '../public')));
app.set('views', path.join(__dirname, '../views'));

正如我在这里读到的那样,它有效,但它对我不起作用,我收到了同样的错误。

标签: node.jsherokudeployment

解决方案


推荐阅读