How does node.js specify .js files to be server-side

126 views
Skip to first unread message

Aleksandra Czajka

unread,
Sep 18, 2014, 1:39:54 PM9/18/14
to nod...@googlegroups.com
you have client-side and server-side js files. how does node.js specify which .js file runs on server and which runs on browser.

Burt Beckwith

unread,
Sep 18, 2014, 5:51:01 PM9/18/14
to nod...@googlegroups.com
It depends on what libraries you use, but in general files aren't loaded automatically for server-side use, they must be require()'d. You can have more complicated startup scripts, but it's common to run node app.js or node server.js (the name doesn't matter) and that's file is your app's entry point. It can have code and logic and depend on other files, or just depend on other files. Those are loaded via require() calls, and they in turn require() other files. This module graph can get large especially with NPM addon modules, but there's not much ambiguity about what gets loaded.

So as long as you keep your server-side code in one area (and in folders that aren't automatically viewable in a browser) and public code in another are and you keep them separate, you're all set. If you want to use a library in both areas (e.g. underscore.js), you'll typically have a build step that configures that for you. Whatever app framework you chose will likely address some of these issues also.

Burt

On Thu, Sep 18, 2014 at 1:39 PM, Aleksandra Czajka <aleks...@gmail.com> wrote:
you have client-side and server-side js files. how does node.js specify which .js file runs on server and which runs on browser.

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/c2709c40-cef8-4ce3-b468-c8193a5eb47c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ryan Schmidt

unread,
Sep 19, 2014, 4:01:06 AM9/19/14
to nod...@googlegroups.com

On Sep 18, 2014, at 12:39 PM, Aleksandra Czajka wrote:
>
> you have client-side and server-side js files. how does node.js specify which .js file runs on server and which runs on browser.

If you're using a web framework like express, you designate a folder to be accessible to the client (e.g. a "public" folder). Files not in that folder cannot be accessed by the client. If you're not using a web framework, then you can program it to work however you want it to.

Aria Stewart

unread,
Sep 19, 2014, 4:01:11 AM9/19/14
to nod...@googlegroups.com

On Sep 18, 2014, at 10:39 AM, Aleksandra Czajka <aleks...@gmail.com> wrote:

> you have client-side and server-side js files. how does node.js specify which .js file runs on server and which runs on browser.

If you send HTML to the browser that references URLs to load JS from, they'll run in the browser; if those URLs map to a node app and it serves them (either as static assets, or with some kind of processing done to them, or generated live), then node is what "decides" to send them to the browser in response, but it's at the browser's request.

Node apps themselves are javascript, so starting from the main script, require() will load files and evaluate them within node.

You can mix the two, or keep them strictly separate -- if you're making an app that has different stuff in the browser than on the server (the norm) than maybe having them in separate folders is useful (often main code in the root, and browser stuff in public/ is what's done)

If you're writing isomorphic modules -- where they work the same in the browser and node, and especially if you do the work to make your app work that way, then mixing them might make more sense. This is a natural thing when you're using tools like browserify to make javascript bundles that run in the browser. Then which is browser and which is 'node' is more a matter of following a dependency chain -- what script starts, and what requires are called and what modules are loaded?

It's okay for them to mix. It's up to you to keep yourself from going nuts when maintaining it though. There's no magic in any of this -- require is a pretty simple function; browserify is more complex but still based on some very simple principles. Browsers requesting javascript is the same as it has been since the <script> tag.

Aria

greelgorke

unread,
Sep 22, 2014, 2:05:45 PM9/22/14
to nod...@googlegroups.com
in short: node.js doesn't specify anything like that. You, the developer, and/or the framework you may be are using on top of that, do define that. There is no magic behind in Node.js Node.js serves Nothing to client, unless you write some code to do so
Reply all
Reply to author
Forward
0 new messages