-3

I'm working on a project that is heavily based on json. Since 90% of the server side in JAVA would be POJO.toJSON() I decided to give NodeJS a shot!

Most of my questions are related to the fact that NodeJS runs as a single thread.

  1. Am I right? :)
  2. NodeJS uses require('something'). Does it mean that I can see the entire app as a single file?
  3. If 2 is yes, a variable declared outside a function should be visible to the entire app. In other words, if I declare var dbConnection = ...; I can access dbConnection from anywhere/anyfile. No connection pool needed?!? Can you please confirm?
  4. If true, a single connection is shared across all the requests. I would expect a huge impact on performances. Can you please clarify?
  5. There is any habit acquired as a JAVA developer that I should avoid? Ex: DTOs seem quite useless if I'm exchanging JSONs with the db (beside documentation purposes of course).
  6. Why I have the feeling that everybody compares Angular to Express when they seem completely different to me? If I understand correctly, Angular seems perfect to exchange JSONs with NodeJS/Express running on the server.

Thanks!

Mark
  • 129
  • 1
  • 11
  • Go through the NodeJS tutorial before asking these questions. and many of these questions have been answered on stackoverflow. – Mox Jun 16 '15 at 06:17

2 Answers2

1

1) Node is using Javascript so it is single threaded and non-blocking.

2) If i understand correctly what you mean.. yes you can have a single file application. Which probably make your life miserable when the app grows big.

3) Yes.This variable will be accessible through out the file. For details and code, regarding the mysql, you could use this module

4)A single connection would create a problem in performance. You'd better use a pool. Info again on the link above.

5) no idea.

6) Angular is client-side and express is server-side, the serve different purposes. Info about express hereand here. Info on angular here

Community
  • 1
  • 1
cs04iz1
  • 1,737
  • 1
  • 17
  • 30
  • Thanks! what I mean with "single file" is that at run time the whole code is downloaded, I'm not planning to write the entire code in one file! :) – Mark Jun 16 '15 at 22:45
0

1) Node.JS is not single threaded. It uses multiple threads. But the JavaScript virtual machine (V8 in most of the cases) is single threaded (in a way, there are ways to make threads and processes). So any call served by JavaScript is mostly single threaded. However, you could create threads, processes and workers under node.js if you like to introduce parallelism in addition to the async programming Node.JS naturally provides

2) I am not quite sure what are you trying to achieve here. If the question is, "could I write my whole code into a single file?", yes you could. If the question is, "is the require loads all the code into a single app and then executes it?", this is not quite right. CommonJS does that, but in Node.JS it is bind to a bit lower level. The CommonJS behavior is emulated, but the performance is a bit higher. Keep in mind, each module runs within its own scope, as well as it is cached (so it is loaded just once, even if you refer to it from multiple locations). Even the main loaded app is actually treated as module and runs within its own scope isolated from the rest. Very little things are present in the Node's "global" scope

3) Look at my answer for question 2. Each module, including your main app starting point are running within different scope. None of them has access to the global "global" scope. There is a such thing, but not made for easy access. Look at this example:

a.js:
globalVar = 1
global.myGlobal = 33
require('./b')

b.js:
console.log(global, global.globalVar, global.myGlobal);
console.log(globalVar)

If there was a global space the way you think it is, if you run node ./a.js you shall get as output the value of globalVar and myGlobal. But you won't. Because each module has it's own global. However, because of the caching (a module is loaded only once), if you need to have a global variable that is accessible from multiple modules and locations, you could do it with a module.

See this example:

a.js:
var b = require('./b');
console.log(b.sharedVariable++);
require('./c');

b.js:
sharedVariable=33;
module.exports = global

c.js:
var b = require('./b');
console.log(b.sharedVariable);

If you run this with node ./a.js you will get 33 and 34. Which is only possible, if the b module is loaded only once internally, and all the other calls just get reference to it. If that was not true, you would have get 33 and 33. This is a good thing. It not only speeds node, the compilation and the loading process, but is also giving you a mechanism to create internal messaging if needed, trough a specially designed module. Anyway, do not implement your modules as with this example. The example is there to prove a point (localized global namespace and caching). There are better patterns for writing modules :)

4) Single connection in what sense? Node does not use single connection, nor provides such. You have a quite good control into your sessions, you can open many connections or reuse the same if you like, or use pools. You can do whatever you like.

5) Do not write synchronous code. Split it into as much smaller tasks as possible and this will keep the execution jitter of your app smaller.

6) I am not having the feeling that Angular is compared to Express. Express is just a popular HTTP path router used in Node.JS (and it is not the only one), while AngularJS is (mostly) a front end UI running at the browser. They work together, not against each other. Even if you use Angular Universal (server side Angular, that pre-compiles part of the Angular code before loading it to the browser) you may still need http router as express at your backend in order to serve your REST queries. They are really different things