node.js: including files

The old way…

By Udo G at Stack Overflow

from: http://stackoverflow.com/questions/5797852/in-node-js-how-do-i-include-functions-from-my-other-files

If, despite all the other answers, you still want to traditionally include a file in a node.js source file, you can use this:

<code>var fs = require('fs'); // file is included here: eval(fs.readFileSync('tools.js')+'');</code>
  • The empty string concatenation +'' is necessary to get the file content as a string and not an object (you can also use .toString() if you prefer).
  • The eval() can’t be used inside a function and must be called inside the global scope otherwise no functions or variables will be accessible (i.e. you can’t create a include() utility function or something like that).

Please note that in most cases this is bad practice and you should instead write a module. However, there are rare situations, where pollution of your local context/namespace is what you really want.

share|improve this answer
answered Apr 27 ’11 at 20:13
Udo G
1,275319
4
Cool, this is useful for quick’n’dirty putting JS libs designed for client-side into a node.js app without the need of maintaining a Node-styled fork. – Kos Dec 11 ’11 at 13:24
1
The “+”” is just a shortcut to force string casting. The more readable solution would be “String(fs.readFileSync(‘tools.js’))” or if the read.FileSync returns an object then “fs.readFileSync(‘tools.js’).toString()” should also work since toString is attached to the base Object prototype (i.e Object.prototype.toString). – Evan Plaice Jan 20 at 23:22
-1 Blindly importing JS into the global namespace can open the code to unintended naming conflicts. If the code in the tools.js file were written in object literal format you could assign them to a local variable which works like a pseudo-namespace. Also, I don’t really think the eval is required (masylum’s code makes it seem like the code is already eval’d during the require). I think you’re import/eval-ing the code with require, casting that code back to string and eval-ing it again just to pidgeonhole it into the global namespace. Looks like you’re doing it wrong. – Evan Plaice Jan 20 at 23:31
1
I just answered the original question, which is about including code, not writing modules. The former can have advantages in certain situations. Also, your assumption about require is wrong: The code is certainly eval’d, but it remains in it’s own namespace and has no way to “pollute” the namespace of the calling context, hence you need to eval() it yourself. In most cases using the method described in my anwer is bad practice but it’s not me that should decide if it is for TIMEX. – Udo G Jan 21 at 17:03
1
@EvanPlaice: do you have a better suggestion which actually answers the question? If you need to include a file which is not a module, do you have a better approach than this? Otherwise, downvoting it seems absurd. – jalf Apr 21 at 11:04

node.js reverse proxy in Ubuntu Apache2

(update) having problems with this when using socket.io from external clients. So it may be worth looking at the alternatives described in the Shelley Powers book on NODE.js

 original post

Here is now to set up a reverse proxy so that a node.js server is visible from the outside world. In our case we redirected any traffic with the subdirectory /nodejs/ to port 8124

for example, this web address http://zerokidz.com/nodejs/

Add the following to the /etc/apache2/sites-enabled/default file: (inside the section already set aside for the virtual host zerokidz)

ProxyPass /nodejs/ http://127.0.0.1:8124/
ProxyPassReverse /nodejs/ http://127.0.0.1:8124/
<Proxy *>
 Order deny,allow
 Allow from all
</Proxy>

So it will look something like this:

<VirtualHost *:80>
  ServerName www.zerokidz.com
  ServerAlias zerokidz.com *.zerokidz.com
  DocumentRoot /home/tkzic/public_html/zerokidz

# Nodejs reverse proxy rule
# sends all traffic for zerokidz.com/nodejs
# to the nodejs server on port 8124

ProxyPass /nodejs/ http://127.0.0.1:8124/
  ProxyPassReverse /nodejs/ http://127.0.0.1:8124/
  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>

</VirtualHost>

Then restart apache, of course

OBD – speeding up data requests – part 2

notes

AT commands which may help speed things up…

AT0, AT1 and AT2 [ Adaptive Timing control ]

When receiving responses from a vehicle, the ELM327 has traditionally waited the time set by the AT ST hh setting for a response. To ensure that the IC would work with a wide variety of vehicles, the default value was set to a conservative (slow) value. Although it was adjustable, many people did not have the equipment or experience to determine a better value.

The Adaptive Timing feature automatically sets the timeout value for you, to a value that is based on the actual response times that your vehicle is responding in. As conditions such as bus loading, etc. change, the algorithm learns from them, and makes appropriate adjustments. Note that it always uses your AT ST hh setting as the maximum setting, and will never choose one which is longer.

There are three adaptive timing settings that are available for use. By default, Adaptive Timing option 1 (AT1) is enabled, and is the recommended setting. AT0 is used to disable Adaptive timing (so the timeout

is always as set by AT ST), while AT2 is a more aggressive version of AT1 (the effect is more noticeable for very slow connections – you may not see much difference with faster OBD systems). The J1939 protocol does not support Adaptive Timing – it uses fixed timeouts as set in the standard.

 

BRD hh [ try Baud Rate Divisor hh ]

This command is used to change the RS232 baud rate divisor to the hex value provided by hh, while under computer control. It is not intended for casual experimenting – if you wish to change the baud rate from a terminal program, you should use PP 0C.

Since some interface circuits are not able to operate at high data rates, the BRD command uses a sequence of sends and receives to test the interface, with any failure resulting in a fallback to the previous baud rate. This allows several baud rates to be tested and a reliable one chosen for the communications. The entire process is described in detail in the ‘Using Higher RS232 Baud Rates’ section, on pages 59 and 60.

If successful, the actual baud rate (in kbps) will be 4000 divided by the divisor (hh).

 Repeated commands

One other feature of the ELM327 is the ability to repeat any command (AT or OBD) when only a single carriage return character is received. If you have sent a command (for example, 01 0C to obtain the rpm), you do not have to resend the entire command in order to resend it to the vehicle – simply send a carriage return character, and the ELM327 will repeat the command for you. The memory buffer only remembers the one command – there is no provision in the current ELM327 to provide storage for any more.

 

S0 and S1 [ printing of Spaces off or on ]

These commands control whether or not space characters are inserted in the ECU response.

The ELM327 normally reports ECU responses as a series of hex characters that are separated by space characters (to improve readability), but messages can be transferred much more quickly if every third byte (the space) is removed. While this makes the message less readable for humans, it can provide significant improvements for computer processing of the data. By default, spaces are on (S1), and space characters are inserted in every response.