所以我正在使用express.js并考虑使用async/await与节点7.有没有办法我仍然可以捕获错误但摆脱try/catch块?也许是函数包装器?我不确定这将如何实际执行函数的代码并调用next(err).
exports.index = async function(req, res, next) {
try {
let user = await User.findOne().exec();
res.status(200).json(user);
} catch(err) {
next(err);
}
}
Run Code Online (Sandbox Code Playgroud)
像这样......?
function example() {
// Implements try/catch block and then handles error.
}
exports.index = async example(req, res, next) {
let user = await User.findOne().exec();
res.status(200).json(user);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
更类似的东西:
var wrapper = function(f) {
return function() {
try {
f.apply(this, arguments);
} catch(e) {
customErrorHandler(e)
}
}
}
Run Code Online (Sandbox Code Playgroud)
这会以某种方式处理try/catch块但不起作用:
exports.index = wrapper(async example(req, res, next) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试全局阻止空用户代理访问服务器上的站点.我添加了一个http_user_agent拒绝,但它根本不起作用.我这样做了吗?这是我的nginx.conf:
#user nginx;
worker_processes 1;
#error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
#pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#tcp_nodelay on;
# enable gzip compression
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/plain application/x-javascript text/xml text/css;
gzip_vary on;
# end gzip configuration
server_tokens off;
server …Run Code Online (Sandbox Code Playgroud)