URL87
URL87

Reputation: 11022

What is the purpose on the middleware Yeoman function implementation?

I'm new in grunt-contrib-connect and came across with this follow middleware function Yoeman implementation -

middleware: function(connect, options, middlewares) {
    return [
        proxySnippet,
        connect.static('.tmp'),
        connect().use('/bower_components', connect.static('./bower_components')),
        connect.static(config.app)
    ];
}

What is the purpose of this implementation ?

Upvotes: 1

Views: 110

Answers (1)

hassansin
hassansin

Reputation: 17508

These are connect middlewares. A middleware is a request callback function which may be executed on each request. It can either modify/end the curent request-response cycle or pass the request to the next middleware in the stack. You can learn more about middlewares from express guide.

In your code you have four middlewares in the stack. First one is for proxying current request to another server. And rest three middlewares are for serving static files from three different directories.

When a request is made to the server, it'll go through these middlewares in following order:

  1. Check if the request should be proxied. If it is proxied to other server, then it's the end of the request/response cycle, rest three middlewares will be ignored.

  2. If not proxied, it'll try to serve the requested file from ./tmp directory.

  3. If the file isn't found in above, it'll look inside ./bower_components. Note that this middleware will be executed only for the requests that has `/bower_components/ in the path. e.g. http://localhost:9000/bower_components/bootstrap/bootstrap.js
  4. Finally, if file isn't found in above two directories, it'll look for it in whatever the path is set in config.app.

That's the end of stack, after that you'll get a 404 Not found error.

Upvotes: 1

Related Questions