Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to route single static files #30

Closed
ivanseidel opened this issue Aug 31, 2014 · 5 comments
Closed

How to route single static files #30

ivanseidel opened this issue Aug 31, 2014 · 5 comments

Comments

@ivanseidel
Copy link

I need to route a single static file, like:

router.get('/admin').to('/admin.html');

Is it possible with barista?

@kieran
Copy link
Owner

kieran commented Aug 31, 2014

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?

@ivanseidel
Copy link
Author

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!)

@kieran
Copy link
Owner

kieran commented Aug 31, 2014

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:

define the route in your router

router.get('/assets/*filename').to('Assets.deliver')

then in your assets controller (app/controllers/assets maybe?):

// 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 :-)

@ivanseidel
Copy link
Author

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?)

@kieran
Copy link
Owner

kieran commented Aug 31, 2014

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants