Express, use with other middlewares?!
Hadryan opened this issue · 7 comments
I'd like to know is that possible to use rocky
along with other middlewares?
"use strict";
var express = require('express')
var bodyParser = require('body-parser')
var rocky = require('rocky')
...
var app = express()
var proxy = rocky()
proxy
.forward('http://new.server')
.replay('http://old.server', { replayOriginalBody: true })
proxy
.get('/api/v1/book/:id')
.toPath('/book/:id', { id: '123' })
})
...
// This is what I tried to achieve, but couldn't find anything near in the Documentations/Examples
app.get("/api/v1/book/:id", app.authentication.Middleware(), app.authorization.Middleware(), proxy.middleware(), function (req, res) {
...
})
...
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port)});
Is that possible at all? if yes, how?
That should be possible.
Try with the following code:
app.get("/api/v1/book/:id", app.authentication.Middleware(), app.authorization.Middleware(), proxy.middleware())
Unfortunately not, it gets only the last middleware. Furthermore, I've some computations with the req
in the
app.get("/api/v1/book/:id", ..., function (req, res) {
// some computations here
})
Although, maybe possible to change some computation, but it's like changing all codes and my app logic totally.
Is there any possibility to plug some foreign functions into proxy
part?
proxy
.get('/api/v1/book/:id')
.toPath('/book/:id', { id: '123' })
.tryToAuthenticate(*request*) // some function to be passed here
.tryToAuthorize(*request*) // another one or even more...
})
I can't see the issue. Express should reach the rocky middleware, try debugging a bit:
const proxyForward = proxy.middleware()
app.get("/api/v1/book/:id", app.authentication.Middleware(), app.authorization.Middleware(), function (req, res, next) {
console.log('Rocky middleware reached:', req.url)
proxyForward(req, res, next)
})
Rocky is middleware-oriented too, so you can do proxy.use(middleware)
, however if you're using express-only middleware, the interface of req/res
vary in some ways and provides convenient sugar which rocky doesn't provides, so I recommend in that case to use express + rocky.
@h2non:
The last solution works, thanks man!
const proxyForward = proxy.middleware()
app.get("/api/v1/book/:id", app.authentication.Middleware(), app.authorization.Middleware(), function (req, res, next) {
console.log('Rocky middleware reached:', req.url)
proxyForward(req, res, next)
})
Now, all three middlewares works as expected.
However please let me a while to debug further to see every things works as normal.
The above solution works flawlessly. However, some modifications need to be added in the code. for example: the global app.use()
sometimes returns no results. I have no time to dig into the rocky's core, but for anyone else who wants quick results, consider: these app wide calls won't work with the above mentioned configuration!
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
instead, use them as route based, something like:
app.use('/login', bodyParser.urlencoded({ extended: true }));
app.use('/login', bodyParser.json());
For sure, the author (@h2non) knows better about these situations, or maybe I missed something, but, that was what fixed my app.
Thanks again for rocky
, the great!
Cheers! If you want to help future rocky users, feel free to make a PR adding some useful example (in examples/
directory) with your approach to use express + rocky + multiple middleware.
Regarding to this issue, I'm gonna close it for now.