我在node.js中有一个应用程序,现在我试图创建一个可执行文件来运行没有所有项目文件的应用程序,但是当我尝试使用pkg
(https://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中,我有这一行可以访问views文件夹:
app.set("views", path.join(__dirname, 'views'));
我该如何解决这种情况?
谢谢
答案 0 :(得分:0)
您将资产路径设置为“脚本”,但必须将此路径设置为“资产”,如下所示:
"pkg": {
"assets": [
"views/**/*",
"public/js/*.js"
],
"targets": "node6"
因为是node.js文件的“脚本”。而且您的应用程序未找到前端js文件。
答案 1 :(得分:0)
您不能使用app.set("views", path.join(__dirname, 'views'));
,因为pkg
将视图放置在快照目录中,因此您的项目将找不到它。
您必须在渲染时明确指出视图的位置
res.render(path.join(__dirname, '..', 'views/index'));