Newbie problem with servlet and res.sendFIle().

109 views
Skip to first unread message

Eric Dykstra

unread,
Mar 21, 2013, 5:39:49 PM3/21/13
to sil...@googlegroups.com
Hello everyone,

Say I have www.somedomain.com/dothis and a servlet called dothis_action().

In that servlet, how do I send a index.jst file to the client?

I tried res.sendFile('path/to/index.jst') and it works fine when the file is really simple and a fully contained (CSS/JS included) file.

However, as soon as I add a line to index.jst to source another file such as:

<script type="text/javascript" src="/desktop/assets/vendor/jquery/jquery-1.9.1.min.js" ></script>

the Webkit console spits out:

Resource interpreted as Script but transferred with MIME type text/html: ....

Uncaught SyntaxError: Unexpected token <

What am I doing wrong?

Thanks,

Eric

Michael Schwartz

unread,
Mar 21, 2013, 6:51:28 PM3/21/13
to sil...@googlegroups.com, sil...@googlegroups.com
You put .jst files in documentRoot like you put .php files for apache.  That is, the server sees .jst and executes it server side. The output of the .jst is what is sent to the browser.

Sent from my iPad
--
You received this message because you are subscribed to the Google Groups "SilkJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to silkjs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Eric Dykstra

unread,
Mar 22, 2013, 7:52:56 AM3/22/13
to sil...@googlegroups.com
Mike,

I have 3 files named index.jst, in different folders. The structure is like this:

/docroot
    /desktop
        index.jst
    /mobile
        index.jst
    config.js
    index.jst
    server.js

Inside server.js, I have functions for:

main_action()
desktop_action()
mobile_action()

Regardless if the request is for /, or /desktop, or /mobile, I want to do some logic inside the appropriate action function, before sending them onto the a index.jst file. That logic is simply checking the DB to see if the requested domain is supported etc. Based on that, I want to redirect them elsewhere, or continue on to the appropriate index.jst file.

Sounds like 'I am doing it wrong' and I need to put this logic inside the index.jst files.

Please clarify.

Thanks.

Michael Schwartz

unread,
Mar 22, 2013, 9:21:20 AM3/22/13
to sil...@googlegroups.com
Right.  

But if really you want to execute JST from inside an action() method, you can.  Look at httpd/child.js at the runJst() method and what it calls.

Eric Dykstra

unread,
Mar 22, 2013, 3:54:09 PM3/22/13
to sil...@googlegroups.com
Works perfect. Thanks Mike!

Michael Schwartz

unread,
Mar 22, 2013, 4:05:31 PM3/22/13
to sil...@googlegroups.com
Your HttpChild.requestHandler method gets first bite at the apple, so to speak.

You can inspect the req and res objects there and do any security checks, etc.  If you return, normal processing occurs.

We talked about using this hook to switch Config.documentRoot based upon the req.host value (vhost!).  You can examine req.uri and do some security checks as well.  If you call res.stop(), no further processing by the normal request handler will be done.

So you could:

res.status = 403;
res.write('Forbidden');
res.stop();

in your requestHandler if you want to deny access.

You can dump the req, res, or any other object with:

console.dir(req);
console.dir(res);
console.dir(some_object);

Eric Dykstra

unread,
Mar 22, 2013, 5:12:37 PM3/22/13
to sil...@googlegroups.com
Mike,

Yep, but when I tried using HttpChild.requestHandler I got an infinite loop. Maybe I misunderstood, but it seemed like I was trying to handle the request before sending it on and that caused it to make a new request which caused the loop.

I could go back and revisit it, but this latest change is simple and works well. 

There should be a standard and documented way to do virtual hosting with SilkJS. Not sure I'm doing it right.

Eric

Michael Schwartz

unread,
Mar 22, 2013, 5:36:28 PM3/22/13
to sil...@googlegroups.com
I had never considered virtual hosting for SilkJS.  YOU came up with the idea to set Config.documentRoot in the requestHandler, and sure enough, that's a brilliant and trivially simple way to do it.

Derek Gransaull

unread,
Mar 24, 2013, 12:03:17 AM3/24/13
to sil...@googlegroups.com
That's the beauty of Open Source ... and to think this is in a thread with a title that starts with "Newbie problem ..." :-)
Reply all
Reply to author
Forward
0 new messages