0

我在 node.js 中有一个应用程序,现在我正在尝试创建一个可执行文件以在没有所有项目文件的情况下运行该应用程序,但是当我尝试使用时遇到问题pkghttps://github.com/zeit/pkg#detecting -assets-in-source-code)。

package.json我添加这个:

"pkg": {
"scripts": "public/js/*.js",
"assets": [
  "views/**/*"
],
"targets": "node6"

},

在控制台中我运行这个命令并且我在这个过程中没有任何错误并创建3个平台可执行文件,pkg index.js --output

当我运行可执行文件时,它开始时没有错误,当我访问浏览器时,它返回此错误:

Error: Failed to lookup view "login" in views directory "/snapshot/Picking/views"
at EventEmitter.render (/snapshot/Picking/node_modules/express/lib/application.js:580:17)
at ServerResponse.render (/snapshot/Picking/node_modules/express/lib/response.js:1008:7)
at ServerResponse.app.use.res.render (/snapshot/Picking/index.js:0)
at index (/snapshot/Picking/controllers/loginController.js:0)
at Layer.handle [as handle_request] (/snapshot/Picking/node_modules/express/lib/router/layer.js:95:5)
at next (/snapshot/Picking/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/snapshot/Picking/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/snapshot/Picking/node_modules/express/lib/router/layer.js:95:5)
at /snapshot/Picking/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/snapshot/Picking/node_modules/express/lib/router/index.js:335:12)

在 index.js 我有这一行来访问视图文件夹:

app.set("views", path.join(__dirname, 'views'));

我该如何解决这种情况?

谢谢

4

2 回答 2

0

您不能使用app.set("views", path.join(__dirname, 'views'));,因为pkg将您的视图放在快照目录中,因此您的项目将找不到它。

您必须在渲染时明确指出您的视图在哪里:

res.render(path.join(__dirname, '..', 'views/index'));
于 2020-11-09T04:07:24.740 回答
0

您将资产路径设置为“脚本”,但必须将此路径设置为“资产”,如下所示:

"pkg": {
"assets": [
  "views/**/*",
  "public/js/*.js"
],
"targets": "node6"

因为 node.js 文件的“脚本”。而且您的应用找不到前端 js 文件。

于 2019-01-16T08:48:07.640 回答