decafjs / decaf-jolt

WWW Server/Application framework for decaf
MIT License
0 stars 1 forks source link

Staticserver for "/" #1

Open mm765 opened 8 years ago

mm765 commented 8 years ago

I tried app.verb('/', new eJoltStaticServer('public')) to kind of move my main directory for public acess one level below my application. My directory structure looks like this: Application |

and i'd like to be able to access the html files by localhost:3001/index.html and localhost:3001/whatever.html is that possible without creating StaticFile()s for every file ?

mschwartz commented 8 years ago

You only need a StaticFile for index.html.

As long as you browser requests /whatever.html, it will get served.

On Thursday, January 7, 2016, mm765 notifications@github.com wrote:

I tried app.verb('/', new eJoltStaticServer('public')) to kind of move my main directory for public acess one level below my application. My directory structure looks like this: Application |

  • public |
  • index.html
  • whatever.html

and i'd like to be able to access the html files by localhost:3001/index.html and localhost:3001/whatever.html is that possible without creating StaticFile()s for every file ?

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf-jolt/issues/1.

mschwartz commented 8 years ago

To clarify.., a StaticFile for / if you want index.html served for / uri

On Thursday, January 7, 2016, Mike Schwartz mykesx@gmail.com wrote:

You only need a StaticFile for index.html.

As long as you browser requests /whatever.html, it will get served.

On Thursday, January 7, 2016, mm765 <notifications@github.com javascript:_e(%7B%7D,'cvml','notifications@github.com');> wrote:

I tried app.verb('/', new eJoltStaticServer('public')) to kind of move my main directory for public acess one level below my application. My directory structure looks like this: Application |

  • public |
  • index.html
  • whatever.html

and i'd like to be able to access the html files by localhost:3001/index.html and localhost:3001/whatever.html is that possible without creating StaticFile()s for every file ?

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf-jolt/issues/1.

mm765 commented 8 years ago

I'd be willing to do the / with a redirect to /index.html I just tried: app.verb('/', new eJoltStaticServer('public')) and tried accessing localhost:3001/whatever.html, which is a file residing in the directory called "public". It does not work (Simply displays "Not found" in the browser) if i do app.verb('public', new eJoltStaticServer('public')) it works by accessing localhost:3001/public/whatever.html Makes me think that "/" cannot be handled by a staticserver ?!

mschwartz commented 8 years ago

Right.

You can have only one / route.

The verbs are looked up in a hash/object. Not iterated over applying regex to each ;)

On Thursday, January 7, 2016, mm765 notifications@github.com wrote:

I'd be willing to do the / with a redirect to /index.html I just tried: app.verb('/', new eJoltStaticServer('public')) and tried accessing localhost:3001/whatever.html, which is a file residing in the directory called "public". It does not work (Simply displays "Not found" in the browser) if i do app.verb('public', new eJoltStaticServer('public')) it works by accessing localhost:3001/public/whatever.html Makes me think that "/" cannot be handled by a staticserver ?!

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf-jolt/issues/1#issuecomment-169868997.

mm765 commented 8 years ago

It was my only "/" route, i commented the other one out. My code looks like this: app = new eJoltApplication() app.verb('/', new eJoltStaticServer('public')) app.listen(3001,'127.0.0.1',10)

mschwartz commented 8 years ago

A verb is required...

/ is special case so / returns the HTML for you SPA without forcing the user to type out the index.html part.

So public might be your verb.

StaticServer will serve public/x.html, public/a/b.html, etc.

It does not do directory index (a/index.html for uri /a/).

On Thursday, January 7, 2016, mm765 notifications@github.com wrote:

It was my only "/" route, i commented the other one out. My code looks like this: app = new eJoltApplication() app.verb('/', new eJoltStaticServer('public')) app.listen(3001,'127.0.0.1',10)

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf-jolt/issues/1#issuecomment-169870806.

mschwartz commented 8 years ago

I do use StaticFile or really SjsFile for my controller URIs.

/events/ lists events /events/seo-name detail page for seo-name event /events/sei-name/edit to edit the event

Similar for /news/

SjsServer for api.

Ajax requests to /api/service.sjs, /api/service2.sjs, etc.

On Thursday, January 7, 2016, Mike Schwartz mykesx@gmail.com wrote:

A verb is required...

/ is special case so / returns the HTML for you SPA without forcing the user to type out the index.html part.

So public might be your verb.

StaticServer will serve public/x.html, public/a/b.html, etc.

It does not do directory index (a/index.html for uri /a/).

On Thursday, January 7, 2016, mm765 <notifications@github.com javascript:_e(%7B%7D,'cvml','notifications@github.com');> wrote:

It was my only "/" route, i commented the other one out. My code looks like this: app = new eJoltApplication() app.verb('/', new eJoltStaticServer('public')) app.listen(3001,'127.0.0.1',10)

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf-jolt/issues/1#issuecomment-169870806.

mm765 commented 8 years ago

I know, i probably didn't explain very good. What i wanted was to server verything from the public directory without having to state the "public" in the url so that localhost:3001/abc.html would serve the file "public/abc.html" and localhost:3001/bcd.html would serve the file "public/bcd.html" for that i wanted to use app.verb('/', staticserver('public')) and this does not seem to work. On the other hand, i just tried app.verb('/'. staticfile('public/whatever.html')) and that works. So re-routing '/' works with staticfile but not staticserver (it seems).

mschwartz commented 8 years ago

URI /verb/a/b/c

Invokes verb handler. In that handler, req.args will be [a,b,c]

On Thursday, January 7, 2016, Mike Schwartz mykesx@gmail.com wrote:

I do use StaticFile or really SjsFile for my controller URIs.

/events/ lists events /events/seo-name detail page for seo-name event /events/sei-name/edit to edit the event

Similar for /news/

SjsServer for api.

Ajax requests to /api/service.sjs, /api/service2.sjs, etc.

On Thursday, January 7, 2016, Mike Schwartz <mykesx@gmail.com javascript:_e(%7B%7D,'cvml','mykesx@gmail.com');> wrote:

A verb is required...

/ is special case so / returns the HTML for you SPA without forcing the user to type out the index.html part.

So public might be your verb.

StaticServer will serve public/x.html, public/a/b.html, etc.

It does not do directory index (a/index.html for uri /a/).

On Thursday, January 7, 2016, mm765 notifications@github.com wrote:

It was my only "/" route, i commented the other one out. My code looks like this: app = new eJoltApplication() app.verb('/', new eJoltStaticServer('public')) app.listen(3001,'127.0.0.1',10)

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf-jolt/issues/1#issuecomment-169870806.

mm765 commented 8 years ago

I understand now, why it doesn't work and will just put it here in my words, to maybe help others who have similar problems in understanding this. The reason it does not work is that when localhost:3001/whatever.html is requested, the verb for comparison, what to execute is actually "whatever.html" because the starting '/' gets removed. So if one does app.verb('/', staticserver('public')), the url to read anything from the public dir is localhost:3001//whatever.html - note the double slash !

@mschwartz one last thing regarding this topic: is there a way to handle all urls that aren't otherwise matched, like having a default route ?

mm765 commented 8 years ago

I looked at the application code and i think it would be relatively easy by having a starndard-default-handler that always returns 404 no matter what it gets passed and then being able to replace it with a different handler (for example a staticserver). Would that work ?

mschwartz commented 8 years ago

// mod rewrite! // this turns /whatever.html into /public/whatever.html app.on('beginReqest', function(req, res) { req.args.unshfit(req.verb); req.verb = 'public'; });

On Fri, Jan 8, 2016 at 7:01 AM, mm765 notifications@github.com wrote:

I understand now, why it doesn't work and will just put it here in my words, to maybe help others who have similar problems in understanding this. The reason it does not work is that when localhost:3001/whatever.html is requested, the verb for comparison, what to execute is actually "whatever.html" because the starting '/' gets removed. So if one does app.verb('/', staticserver('public')), the url to read anything from the public dir is localhost:3001//whatever.html - note the double slash !

@mschwartz https://github.com/mschwartz one last thing regarding this topic: is there a way to handle all urls that aren't otherwise matched, like having a default route ?

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf-jolt/issues/1#issuecomment-170025323.

mschwartz commented 8 years ago

You can install your own 404 handler and handle default however you like.

Also note that res.redirect() does return. This allows you to do additional logic once the client is busy doing its redirect.

I found that if you are doing any bookkeeping, like counting certain things, in the database, you might want to do that logic after the redirect.

If you want to add page logging, you'd log the page request after the redirect.

And so on.

You can use res.stop() anywhere to end the request and terminate request processing. It still honors connection keep-alive; it is not a hard socket close or anything bad.

On Fri, Jan 8, 2016 at 7:18 AM, mm765 notifications@github.com wrote:

I looked at the application code and i think it would be relatively easy by having a starndard-default-handler that always returns 404 no matter what it gets passed and then being able to replace it with a different handler (for example a staticserver). Would that work ?

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf-jolt/issues/1#issuecomment-170029171.

mm765 commented 8 years ago

Yeah that would work(verb changing in beginrequest) - i would have to do my own regex matching then, though, to only replace the verb for those files i want it to. I think i like the default handler better - will try to see if i get that to work .. Thanks again for your nice and quick support, it is really appreciated !

mschwartz commented 8 years ago

beginRequest gets you first bite at the request/response objects, before Jolt does its thing. Rewrite is only one possibility. You might want to perform user authentication and decorate the req object with the logged in user information.

Or you might implement server-side sessions a la PHP as req.session. You can save the session data in the endRequest handler.

endRequest is a good place to log requests, as apache does with its access_log files.

On Fri, Jan 8, 2016 at 7:27 AM, mm765 notifications@github.com wrote:

Yeah that would work(verb changing in beginrequest) - i would have to do my own regex matching then, though, to only replace the verb for those files i want it to. I think i like the default handler better - will try to see if i get that to work .. Thanks again for your nice and quick support, it is really appreciated !

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf-jolt/issues/1#issuecomment-170031367.

mm765 commented 8 years ago

I use beginrequest already for session handling. I looked at the Application.js again and was wondering what the verb[404] is for. It seems to me to be exactly what i want, except for that it doesn't shift the verb into the args and so it doesn't work with the staticserver out of the box. And that does not really make sense because the handler function doesn't get passed the verb, so you can't actually handle the request in that verb[404] because you do not really know which url was accessed. I think it would make sense to call that with the verb shifted into the first arg and that way the staticserver would work with it and it would be the defaulthandler that i was thinking of. For handling the errors and default-404 handling there still would be the errorhandler. What do you think ?

mm765 commented 8 years ago

Just in case someone needs the same that i did: I changed Application.js lines 172 and following to this: config = verbs[verb]; if (config) { status = config.handler(config, req, res); } else { if(verbs[404]) { config = verbs[404]; req.args.unshift(req.verb); status = config.handler(config, req, res); } else { status = 404; } }

And using app.verb(404, new eJoltStaticServer('public/')) it works with staticserver handling everything that doesn't have a verb defined and serving files from my public directory.

Thanks for the hint with the unshift(verb) which makes this a really simple change.

mschwartz commented 8 years ago

You can examine req.uri, too :)

On Sat, Jan 9, 2016 at 1:08 AM, mm765 notifications@github.com wrote:

I use beginrequest already for session handling. I looked at the Application.js again and was wondering what the verb[404] is for. It seems to me to be exactly what i want, except for that it doesn't shift the verb into the args and so it doesn't work with the staticserver out of the box. And that does not really make sense because the handler function doesn't get passed the verb, so you can't actually handle the request in that verb[404] because you do not really know which url was accessed. I think it would make sense to call that with the verb shifted into the first arg and that way the staticserver would work with it and it would be the defaulthandler that i was thinking of. For handling the errors and default-404 handling there still would be the errorhandler. What do you think ?

— Reply to this email directly or view it on GitHub https://github.com/decafjs/decaf-jolt/issues/1#issuecomment-170212687.