Closed ivanseidel closed 10 years ago
If your needs are simple, you can probably start with something like https://gist.github.com/rpflorence/701407 or https://github.com/cloudhead/node-static
Are you using barista for anything else in your project? What framework are you using?
I'm using it for geddy.js
My routes are complex (REST and so on), but i'm building it with an Angular app, that only requires static assets. I didn't want the user to go to /admin.html#/<whatever>
, but /admin/#/<whatever>
.
Also, putting it inside another folder possible for my architecture...
(thanks for the fast answer!)
So, first of all, routes with local segments (hash links) will never be sent to the server. Everything before the #
is the part of the URL that will be requested from your server, then it's up to you (and angular) what to do with the hash route (everything after the #
).
That said, you can absolutely use geddy / barista to resolve static assets.
I'd probably set up a static asset controller and deliver the files that way. Something like this:
router.get('/assets/*filename').to('Assets.deliver')
// include fs and path from node
var fs = require('fs'),
path = require('path');
// your geddy `Assets.deliver` method
this.deliver = function (req, resp, params) {
// build the filepath from the request params
var filePath = path.join(__dirname, params.filename);
// find the file on your server
var stat = fs.statSync(filePath);
// write the response headers
resp.writeHead(200, {
'Content-Type': 'application/whatever',
'Content-Length': stat.size
});
// create a read stream and pipe it to the client
var readStream = fs.createReadStream(filePath);
readStream.pipe(resp);
}
Keep in mind that's an extremely simple example with zero security considerations or testing, but should at least put you on the right track.
Also don't be afraid to ask these types of questions in the #geddy room on IRC! They're a friendly bunch :-)
That was my plan B haha
Security is not the problem, since all my REST endpoints will be protected, allowing the user to load the admin page is the best he can do if trying to invade...
Will do that, thanks! (where is the IRC room?)
This is a good, free, web-based IRC client: https://www.irccloud.com
After you're connected, join the #geddy channel (which is on Freenode, if it asks)
I need to route a single static file, like:
router.get('/admin').to('/admin.html');
Is it possible with barista?