来了一系列代码执行,我发现这里不寻常的是代码:
server.js
?x=randomStr
Actions_single_PVC.js
#the head of the dataset after deleting ID attribute
head(wpdc)
#k fold cross validation
RF_folds<-createFolds(wpdc$outcome, k=10) #create folds
RF_fun <- lapply (RF_folds, function(x){
RF_traing_folds=wpdc[-x,]
RF_test_folds=wpdc[x,]
RF_test_folds_class<-RF_test_folds[,1]
#build the model
RF_model<-randomForest(outcome ~ ., data = RF_traing_folds)
#test the model
RF_predict<-predict(RF_model, RF_test_folds[-1])
#accuracy
RF_table<-table(RF_test_folds_class,RF_predict)
RF_confusionMatrix<-confusionMatrix(RF_table, positive ="R") #to see the matrex of echo floods
return(RF_confusionMatrix$table)
})
RF_sum_matrices <-Reduce('+', RF_fun)/10 #sum 10 matrices
RF_final_confusionMatrix<-confusionMatrix(RF_sum_matrices, positive ="R")
RF_final_confusionMatrix
如果你之前从未见过这种结构,那就是asyncMW:
const Actions_Single_PVC = require('./routes/Actions_single_PVC.js');
app.use('/Actions_single_PVC', Actions_Single_PVC);
app.use((err, req, res, next) => {
console.log('invalid token');
});
我不理解的是,当抛出错误(我在此处使用router.post('/', asyncMW(async (req, res, next) => {
throw new Error();
}));
router.use((err, req, res, next) => {
console.log('error');
}
重现)时,const asyncMiddleware = fn =>
(req, res, next) => {
Promise.resolve(fn(req, res, next))
.catch(next);
};
module.exports = asyncMiddleware;
文件中的错误处理中间件被执行。我预计throw new Error();
的错误处理中间件会被执行。
为什么server.js
中的错误middlware被执行而不是Actions_single_PVC.js
中的错误middlware?
答案 0 :(得分:4)
这是因为以下代码仅将中间件应用于基本路径匹配Actions_single_PVC
的请求。
app.use('/Actions_single_PVC', Actions_Single_PVC);
以下代码将中间件应用于所有全局请求。
app.use((err, req, res, next) => {
console.log('invalid token');
});
如果您点击了网址/Actions_single_PVC
,则Actions_single_PVC
中的中间件会被点击。