【发布时间】:2020-10-26 11:12:48
【问题描述】:
我正在创建一个 express.js 应用程序作为团队项目的一部分。我是 javascipt 新手,但任务最终落在了我身上。相关代码的目的是在单击按钮时运行脚本,使用一些用户定义的选项,然后显示新页面并显示在结果页面上生成的报告的链接。无论出于何种原因,这在应用程序启动后第一次都不会起作用,但如果您返回并重试,它就会起作用。我曾认为存在同步问题,并且可能存在,但似乎也存在数组变量未传递给 pug 的问题。几个星期以来,我一直把头撞在桌子上,并向我的教授(我们都不是 CS 人员)寻求帮助,但没有运气。请帮忙。
这里是文件开头的app变量、配置等:
// index.js
/**
* Required External Modules
*/
const express = require("express");
const path = require("path");
const shell = require("shelljs");
const fs = require("fs");
/**
* App Variables
*/
const app = express();
const port = process.env.PORT || "8000";
var ipAddresses;
var ipAddressesLink;
/**
* App Configuration
*/
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");
app.use(express.static(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname, "reports/html")));
//code to make html forms work
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
以下是一直让我失望的相关路线:
//Run script when post is rec'd from root and send to results page
app.post("/", (req, res) => {
//take values and create complete command for Astrum script
var commandString = 'bash /home/astrum/Main/Astrum.sh -s ' + req.body.speed + ' -h ' + req.body.host + ' -u ' + req.body.username + ' -p ' + req.body.password;
var pathToReports = './reports/html';
runScript(commandString);
readFolder(pathToReports);
renderPage();
//Iterate thru filenames to create arrays for links and link labels
function readFolder(pathValue) {
fs.readdir(pathValue, (err, files) => {
console.log(files)
//variable & method for links to html records pages
ipAddressesLink = files;
console.log(ipAddressesLink);
//variable and method to remove file extension for link labels in pug
ipAddresses = files.map(removeExtension);
});
}
//function to remove last five characters of each element
function removeExtension(value) {
return value.substring(0, value.length - 5);
};
//function to render the page
function renderPage() {
res.render("results", {ipAddressesLink, ipAddresses, title: 'Results'});
}
//function to execute command in shell
function runScript(value) {
shell.exec(value);
}
//show array on console for debugging
console.log("type of record is: " + typeof ipAddressesLink);
console.log(ipAddressesLink);
console.log(ipAddresses);
res.end();
});
这是引发错误的结果页面的 pug 模板,显然正在进行中:
extends layout
block layout-content
div.View
div.Message
div.Title
h1 Astrum Network Analysis
div.Body
div.multiple-group
h3 Heading
select(id='whitelist', name='whitelist' size='6' multiple)
option(value="volvo") Volvo
option(value="saab") Saab
option(value="fiat") Fiat
option(value="audi") Audi
option(value="bmw") BMW
div.form-group
label(for='whitelistButton')
input(type='submit' value='Whitelist Ports')
h3 Hosts Found:
ul
each val, index in ipAddressesLink
li: a( href = val ) #{ipAddresses[index]}
这是我收到的错误消息:
TypeError: /home/astrum/Main/astrumApp/views/results.pug:36
34| ul
35|
> 36| each val, index in ipAddressesLink
37|
38| li: a( href = val ) #{ipAddresses[index]}
39|
Cannot read property 'length' of undefined
at eval (eval at wrap (/home/astrum/Main/astrumApp/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:93:32)
at eval (eval at wrap (/home/astrum/Main/astrumApp/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:116:4)
at template (eval at wrap (/home/astrum/Main/astrumApp/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:119:7)
at Object.exports.renderFile (/home/astrum/Main/astrumApp/node_modules/pug/lib/index.js:452:38)
at Object.exports.renderFile (/home/astrum/Main/astrumApp/node_modules/pug/lib/index.js:442:21)
at View.exports.__express [as engine] (/home/astrum/Main/astrumApp/node_modules/pug/lib/index.js:491:11)
at View.render (/home/astrum/Main/astrumApp/node_modules/express/lib/view.js:135:8)
at tryRender (/home/astrum/Main/astrumApp/node_modules/express/lib/application.js:640:10)
at Function.render (/home/astrum/Main/astrumApp/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/home/astrum/Main/astrumApp/node_modules/express/lib/response.js:1012:7)
【问题讨论】:
-
您确定文件不是空的吗?
console.log(ipAddressesLink)会打印什么?
标签: javascript node.js express pug