1

If a user navigates to a URL in your Meteor app, such as "platypus.meteor.com/nfnoscar", is there an event that can read information about the user's device, such as via an HttpRequest object or something?

IOW, what, if any, information about the requester's context is available when an URL is navigated to? Can the requesting device's capabilities be read? Anything?

UPDATE

I tried to test MasterAM's idea with this code:

Template.garrapatabeach.rendered = function() {
   var req = request;
   alert(req);
}

...but I get:

=> Exited with code: 8
. . .
W20151012-09:48:06.548(-7)? (STDERR) ReferenceError: Template is not defined
W20151012-09:48:06.549(-7)? (STDERR)     at meatier.js:8:1

meatier.js line 8 is:

Template.garrapatabeach.rendered = function() {

I do have a template with that name:

<template name="garrapatabeach">

...so I don't know what the complaint is... Surely it knows what "Template" itself is. For full disclosure, here's the entire console dump of the error:

=> Exited with code: 8
W20151012-09:48:06.543(-7)? (STDERR)
W20151012-09:48:06.547(-7)? (STDERR) C:\Users\clayshan\AppData\Local\.meteor\pac
kages\meteor-tool\1.1.9\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\
fibers\future.js:245
W20151012-09:48:06.547(-7)? (STDERR)
throw(ex);
W20151012-09:48:06.548(-7)? (STDERR)
      ^
W20151012-09:48:06.548(-7)? (STDERR) ReferenceError: Template is not defined
W20151012-09:48:06.549(-7)? (STDERR)     at meatier.js:8:1
W20151012-09:48:06.549(-7)? (STDERR)     at C:\Misc\meatier\.meteor\local\build\
programs\server\app\meatier.js:32:4
W20151012-09:48:06.550(-7)? (STDERR)     at C:\Misc\meatier\.meteor\local\build\
programs\server\boot.js:242:10
W20151012-09:48:06.550(-7)? (STDERR)     at Array.forEach (native)
W20151012-09:48:06.550(-7)? (STDERR)     at Function._.each._.forEach (C:\Users\
clayshan\AppData\Local\.meteor\packages\meteor-tool\1.1.9\mt-os.windows.x86_32\d
ev_bundle\server-lib\node_modules\underscore\underscore.js:79:11)
W20151012-09:48:06.551(-7)? (STDERR)     at C:\Misc\meatier\.meteor\local\build\
programs\server\boot.js:137:5
W20151012-09:48:23.969(-7)? (STDERR)
W20151012-09:48:23.970(-7)? (STDERR) C:\Users\clayshan\AppData\Local\.meteor\pac
kages\meteor-tool\1.1.9\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\
fibers\future.js:245
W20151012-09:48:23.971(-7)? (STDERR)
throw(ex);
W20151012-09:48:23.971(-7)? (STDERR)
      ^
W20151012-09:48:23.971(-7)? (STDERR) ReferenceError: Template is not defined
W20151012-09:48:23.972(-7)? (STDERR)     at meatier.js:8:1
W20151012-09:48:23.972(-7)? (STDERR)     at C:\Misc\meatier\.meteor\local\build\
programs\server\app\meatier.js:32:4
W20151012-09:48:23.972(-7)? (STDERR)     at C:\Misc\meatier\.meteor\local\build\
programs\server\boot.js:242:10
W20151012-09:48:23.973(-7)? (STDERR)     at Array.forEach (native)
W20151012-09:48:23.973(-7)? (STDERR)     at Function._.each._.forEach (C:\Users\
clayshan\AppData\Local\.meteor\packages\meteor-tool\1.1.9\mt-os.windows.x86_32\d
ev_bundle\server-lib\node_modules\underscore\underscore.js:79:11)
W20151012-09:48:23.973(-7)? (STDERR)     at C:\Misc\meatier\.meteor\local\build\
programs\server\boot.js:137:5
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    That depends entirely on the routing code. Navigation may or may not trigger a DDP connection. I did not test this myself, but the `request` (node's request object) should be available to the server. In any case, I don't think that navigation is an action that guarantees information exchange between the client and server, as opposed to traditional web applications. Are you aiming for analytics of some sort? – MasterAM Oct 12 '15 at 16:33
  • That would be great, thanks! – B. Clay Shannon-B. Crow Raven Oct 12 '15 at 18:31
  • Do you mind if I edit out your attempt and freshen up your question a bit? – Kyll Oct 13 '15 at 18:28

1 Answers1

1

Using the webapp package you can access the HTTP connection handlers of Meteor.
You can hook your own logic to the connection handlers as so :

WebApp.rawConnectHandlers.use('/somepath', (req, res, next) => {
  console.log(`received request with headers : ${req.headers}`)
  next()
})

The docs advertise connectHandlers but I could never make those work!
See more about arrow functions and Template Strings ES2015 magic

As per the docs the first argument to the callback is a NodeJS HTTP Incoming message and the second argument is obviously a NodeJS HTTP Server Response. The third argument is the function you have to call once you are done, various router technologies use a similar strategy (iron:router, ExpressJS, ...).

Since you have access to the raw request you can access all the information that Node gives you (host, user-agent, ...), and you can alter the response to change cache settings for example.

To implement this I suggest going through a package approach to ensure that the alteration of these connection handlers happen when you want, before your application loads up.
It will also allow you to reuse it anywhere.

Finally, note that the webapp package is by its very nature only available on the server.

Community
  • 1
  • 1
Kyll
  • 7,036
  • 7
  • 41
  • 64