What determines whether a middleware is "connect-compatible" or not? - node.js

Recently while traversing through the Next.js docs, specifically middleware for API routes, it states the following:
You can also use Connect compatible middleware.
I'm not too familiar with express/connect, so I'm wondering what makes a piece of middleware connect compatible? It doesn't seem to be something that is called out in the docs of middleware specifically. Is there a certain signature that applies? Or is this related to Node.js and the (req, res) pattern?
Any insight would be greatly appreciated! Thanks!

Related

Nodej.s routing with express.js and TypeScript

I have question about node.js routes. Which routing version is correct?
First version it's a standard version in express.js:
router.get('/packages/:name', (req, res) => {
//my example code
);
Second version with TypeScript. This version is from typeorm init command.
export const Routes = [{
method: "post",
route: "/user",
controller: CustomerController,
action: "createUser"
}];
Which version is better and why? About the second version, how i can add custom middleware? What is difference between first and second version?
Both the approaches are same. When you have a lot of routes for a single point like root/apiv1/[here all the routes] then the second
one is preferable, if you have many dynamic routes, so its better to
go with the first approach.
Talking about the language, you can achieve both kind of routing in
plane JS and also in JS. But due to typecasting and validations,
preferred language is typescript and way of routing depends on the situation.
Talking about the middleware, for the first approach we will pass the
middleware just before the controller function, and for the second
approach, we are bascially creating structures for our routes and we need to
pass these routes to some route() end point, there we will
define the middleware just like we are doing in the first approach.

using connect middlewares with geddyjs

I'm would like to use some middleware of connect in my geddy applications. My question is, is it possible to use connect middlewares with geddyjs?
Thanks
Yes, there is a Connect compatibility mode (set connectCompatibility = true in your app config), which causes Geddy's before-filters to behave like middleware -- but it's not well tested.
Geddy's baked-in auth (geddy-passport) uses Passport, which is structured as Connect middleware, so even without this compat-mode, middleware can be made to work, with a little bit of effort. If you have issues you can always post on Geddy's mailing list (https://groups.google.com/group/geddyjs), or hop into IRC (#geddy on Freenode).

Express JS equivalent of decorator pattern from Python frameworks

Working with Express js to write a simple NodeJS webservice. I'm historically a python guy.
In frameworks like Django or Flask, its common to see Python decorators used to implement logic from plugins only on specific endpoints. An example of this pattern can be seen here.
http://pythonhosted.org/Flask-Classy/#using-multiple-routes-for-a-single-view
I'm working on an Express middleware and have everything working well with the app.use 3-parity function, but this is only relevant for logic executes for every request. I'd like to allow the end user of the plugin to run parcels of my logic (already in separate functions) only on specific endpoints similar to the pattern outlined in the source above.
Some of the configuration to these wrappers would be passed at app start.
What would be the best approach to this? Should I emulate this pattern with functions that take the actual route handler as an argument and return it at end? Something like this?
function pluginWrapper(endptFunc){
//plugin logic here
return endptFunc;
}
app.get('/endpt', pluginWrapper(function(req,res,next){
//endpt logic here
res.end()
}));
Here are the express idiomatic strategies:
Things relavent for the majority of requests across the entire site become normal connect middleware: app.use(express.cookieParser())
Things relavent for just a particular route can go just on that route: app.post('/users', express.bodyParser(), createUser). This is the pattern I think that most closely matches your above scenario
Groups of related middleware can be passed as lists: app.get('/books', [paginate, queryLimit, memoize], getBooks). And of course that list could be a variable or module and thus shared in a DRY fashion.
Common functionality triggered by patterns in the path itself can use app.param: app.get('/:username/hobbies', getHobbies)
Existing regular functions can be wrapped into middleware to adapt them to a middleware API.
You can just call functions as normal. Not every method of code reuse has to be shoehorned into one of express's convenient patterns.
To address your question more directly, I don't think you should try to port the python decorator pattern 1-to-1 to javascript. Middleware accomplishes essentially the same thing. If you post concrete examples using decorators, we can suggest an idiomatic way to implement them in express.

Express & Socket.io Route parroting/copying/sharing

I'm working with expressjs and socket.io in nodejs. I'm looking into assign identical route handlers to requests made in either HTTP or via websockets/socket.io.
For instance:
var responder = function(req, res){
req.params //<-- {id: 'something...'}
}
app.get('/foo/:id', responder);
io.on('/foo/:id', responder);
socket.io doesn't appear to have this type of routing functionality. Does anyone know of a library/module to help with this?
There are several options.
If you'd like to keep using express, check out express.io.
If you don't mind using something a bit different, sails lets you do this sort of thing as well.
(Update: sails now uses express too)
Both have been used in production successfully.
Note that routing is also pretty simple to implement on your own. If you check out how express do it I'm sure you'll be able to figure out a slim implementation that would match you needs.
Good luck! Let me know what you ended up using and how it worked for you.

Where is Express.js' API?

have known node.js and express several days ago. I feel they are really cool, however, lacking of something.
I looked the official site, but it just point to Connect's API.
But I can't find a document which teach me how to use a simple function such as expressHTTPServer.get().
Of course I read the nodejs's API, but the Express and Connect seems to extend them very much.
For example, in Express official site:
app = express.createServer();
app.get('/user/:id', function(req, res, next){
loadUser(req.params.id, function(err, user){
if (err)
return next(err);
res.send('Viewing user of csser.com ' + user.name); }
);
});
But sadly, no API document talk me there is expressHTTPServer.get and what it's callback arguments are.
I like to read examples, but I really need an API to learn a new language/library/framework, anyone helps?
Here are the official docs: http://expressjs.com/guide.html
Also, the source can answer a lot of your questions: https://github.com/visionmedia/express
If I understand your question correctly, you want to get to know the API of the req and res parameters passed to your callback, right?
Have a look at http.ServerRequest and http.ServerResponse
Express itself uses Connect which uses the standard Node.js HTTP API. The arguments passed to your callback are monkey patched instances of the described objects.
The Argument "next" is a function you can call if you wish that the request gets handled by another middleware module. If you want to handle the request within your handler, this doesn't need to bother you.

Resources