Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Express中间件多次调用_Javascript_Node.js_Express_Middleware_Express Router - Fatal编程技术网

Javascript Express中间件多次调用

Javascript Express中间件多次调用,javascript,node.js,express,middleware,express-router,Javascript,Node.js,Express,Middleware,Express Router,我已经查看了其他与此主题相关的帖子,但在我的代码中找不到问题 const myMiddleware = (fn) => { return (req, res, next) => { var fullUrl = req.protocol + '://' + req.get('host') + req.url; console.log(fullUrl) next() } } const app = express() a

我已经查看了其他与此主题相关的帖子,但在我的代码中找不到问题

const myMiddleware = (fn) => {
    return (req, res, next) => {
        var fullUrl = req.protocol + '://' + req.get('host') + req.url;
        console.log(fullUrl)
        next()
    }
}

const app = express()

app.use('/dist', express.static(__dirname + '/client/dist'))
app.use('/static', express.static(__dirname + '/client/static'))

app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParserMiddleware())
app.use(passport.initialize())

const server = https.createServer(options, app)

app.get('/', myMiddleware((req, res) => {
    res.sendFile(__dirname + '/client/dist/index.html')
}))

app.all('*', (req, res) => {
    res.redirect('/')
})

server.listen(8080, function listening() {
    console.log('Listening on %d', server.address().port)
})
如果没有
路径上的
myMiddleware
路径,一切都会正常工作。通过将
myMiddleware
附加为
app.get('/',myMiddleware((req,res)=>{
然后多次调用
myMiddleware
,而不调用
res.sendFile('/client/dist/index.html')


编辑:jfriend00的解决方案修复了下面的错误。多次调用的中间件仍然存在。原因是favicon和其他一些资产没有被
app捕获。使用('/static',express.static(uu dirname+'/client/static'))
line。修复路径也解决了第一个错误

除此之外,我还尝试删除下面的部分,但应用程序根本不起作用。我猜这里有两个bug

app.all('*', (req, res) => {
    res.redirect('/')
})
我已经发布了一张图片,展示了在删除
app.all('*')


我想在这里猜一猜

更改此项:

app.get('/', myMiddleware((req, res) => {
    res.sendFile(__dirname + '/client/dist/index.html')
}));
为此:

app.get('/', myMiddleware(), (req, res) => {
    res.sendFile(__dirname + '/client/dist/index.html')
}));
因此,在
myMiddleware
调用
next()
之后,实际上会调用
res.sendFile()


然后,删除
fn
参数,这样就有了它(不会改变执行,但会删除误导性和未使用的参数):


你的中间件编写不正确。我不知道你想用它做什么,但你传递给它的回调从未被使用过。它所做的只是调用
next()
@jfriend00通常内部有编写的代码,但为了找出错误,我简化了它进行测试。你可以假设它有console.log()或者其他一些内部逻辑你是否意识到
app.get('/',myMiddleware((req,res)=>{…});
从来没有做过任何事情?它所做的只是调用
next()
。这是我的观点。我不知道你打算它做什么,但它在你的代码示例中没有任何作用。我不能说出你的问题到底是什么,所以我不知道这是否与问题相关。因此,你的代码所做的就是重定向到
/
,重定向到
/
,重定向到
/
等等。它从来不会做任何其他事情,而是一个无限循环,直到浏览器可能检测到太多重定向。想象一下,它会做些什么。我编辑代码以打印url只是为了你。没有中间件,就没有重定向循环,应用程序也可以工作。使用任何中间件,都会有重定向循环。你需要一个做其他事情的路由重定向。你没有显示一个。因此,你的服务器所做的只是一次又一次地重定向。如果你无法获得该重定向或向我们显示更多真实代码,那么我将继续。我提到了两个错误。(1)多次调用中间件(2)删除
app.all('*',(req,res)=>{
将导致错误。应用您的更改后,我们修复了此错误#2,但是,调用localhost:8080将调用
myMiddleware
3times@Thellimist-这并不奇怪。对服务器的一个调用将是favicon。您的HTML页面中是否有任何
标记或其他资源引用,以便进行其他操作r调用您的服务器。您正在中间件中记录URL。您可以看到请求的内容,对吗?我有
static/css/lobb.css
static/css/style.css
和favicon。他们调用是正常的还是有更好的方法?@thellimit-浏览器就是这样获取这些文件的。它会向您的服务器发出请求这就是网页的工作方式。他们正在调用
static/
路径,因此我假设
express.static
中间件将捕获该请求,而不会将其一直传递到
myMiddleware
。我是否遗漏了什么?
const myMiddleware = () => {
    return (req, res, next) => {
        var fullUrl = req.protocol + '://' + req.get('host') + req.url;
        console.log(fullUrl)
        next()
    }
}