NodeJS在以百分号开头的路由上抛出错误

时间:2014-11-03 00:37:39

标签: javascript node.js express

我有一个服务器编写NodeJS,后端设置就像这样

app.use(express.static(__dirname))

app.get('/', function(req, res){
  res.send('main page here')
});

app.get('/*', function(req, res){
  res.send('404 dude')
});

app.use(function(err, req, res, next){
  console.error(err.stack);
});

当我在/%点击我的服务器时,我得到了这个堆栈跟踪:

Error: Bad Request
    at SendStream.error (/Users/danielchao/code/node/node_modules/express/node_modules/send/index.js:239:16)
    at SendStream.pipe (/Users/danielchao/code/node/node_modules/express/node_modules/send/index.js:411:32)
    at serveStatic (/Users/danielchao/code/node/node_modules/express/node_modules/serve-static/index.js:107:12)
    at Layer.handle [as handle_request] (/Users/danielchao/code/node/node_modules/express/lib/router/layer.js:82:5)
    at trim_prefix (/Users/danielchao/code/node/node_modules/express/lib/router/index.js:270:13)
    at /Users/danielchao/code/node/node_modules/express/lib/router/index.js:237:9
    at Function.proto.process_params (/Users/danielchao/code/node/node_modules/express/lib/router/index.js:312:12)
    at /Users/danielchao/code/node/node_modules/express/lib/router/index.js:228:12
    at Function.match_layer (/Users/danielchao/code/node/node_modules/express/lib/router/index.js:295:3)
    at next (/Users/danielchao/code/node/node_modules/express/lib/router/index.js:189:10)

为什么我的静态资产处理程序错误?理想情况下,这应该作为404来处理。

1 个答案:

答案 0 :(得分:0)

%是网址中的转义字符。

如果您想在网址中使用文字%,请使用此字词:%25