0

I'm in a team project, and my team is slacking, so I'm going to take the lead and start off the repository. We are all new to node.js and come from other backgrounds.

There is only 1 "server," right?

So the main file (unsure of what's a standard name to call it is), would hold this code:

var server = http.createServer(
    function( request, response ){

Then all team members would write their own JS files for their respective parts, and we'd use something like this to use them in the server file?

<script src="1.js"></script>
<script src="2.js"></script>

For some reason it just seems weird to me that a majority of the code will be in a single "server" file.

User
  • 23,729
  • 38
  • 124
  • 207

2 Answers2

2

No.

Typically you would have a single server, but you would usually split its functionality up among different modules which you would import using require. Often you would use a framework such as Express rather than using the HTTP module directly.

The HTML <script> element is used to load client side JavaScript into an HTML document. It isn't used for server side JavaScript with NodeJS at all.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Correct, we're using express. – User Apr 03 '16 at 22:27
  • @User — The code you have looks more like the basic HTTP module than it does Express. – Quentin Apr 04 '16 at 07:08
  • @Quentin, but dose that mean i have to write get and post code in main file and pass its parameters to other functions inlcuded through modules. Like if i have 10 files and each file has approx 20 functions then my nodejs main file will contain 200 get or post functions in single file ? – Purushottam zende Nov 23 '16 at 17:22
0

You can split your code in different files. Then, you can "import" with the function "require".

You can import one file, functions or a directory.

You can read more here https://nodejs.org/api/modules.html

This

<script src="1.js"></script>

is for loading code in the client (in a webpage).

I hope this answer will help you!

iblancasa
  • 334
  • 1
  • 5