Reputation: 58619
I have some folders in my source that I want to serve up using connect via a grunt task. My folder structure is as follows...
My grunt configuration looks something like this...
grunt.initConfig({
connect: {
options: {
port: 8080,
hostname: '0.0.0.0',
livereload: 35729
},
app: {
options: {
middleware: function (connect) {
return [
connect.static(require('path').resolve(pkg.paths.dist)),
connect.static(require('path').resolve(pkg.paths.src)),
connect.static(require('path').resolve(pkg.paths.docs))
];
}
}
},
}
})
Firing up a server, and visiting http://localhost:8080/
will give me the index.html
file from dist
- which has been compiled from index.jade
, which refers to main.css
which is dutifully served up from src
. This is all excellent, and works great.
Now I want to access the index.html
file from docs
, but on an aliased url - so http://localhost:8080/mycustomurl
. I don't want to put my docs in a sub-folder, I just want to configure connect to serve urls matching mycustomurl
from the docs
directory.
How can I modify my configuration to achieve this?
Upvotes: 3
Views: 1355
Reputation: 3460
Use a custom middleware. middleware
option expects a function that returns an array of middlewares.
custom_middleware: {
options: {
middleware: function(connect, options, middlewares) {
return [connect.static(require('path').resolve(pkg.paths.dist)),
connect.static(require('path').resolve(pkg.paths.src)),
function (req, res, next) {
if (req.url !== '/custom/url') {
next();
return;
}
// res.sendFile(pkg.paths.docs + '/index.html');
// you can access the "anything.html" by parsing the req.url
var file = req.url.split('/');
file = file[file.length-1];
res.sendFile(pkg.paths.docs + file);
}
];
}
}
}
For more configuration options see example Gruntfile.
Upvotes: 2