0

I've just installed Request module in my express + node.js project and I would like to know in which folder I should place my custom js file that will contain the following code that basically executing Request module.

var request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Show the HTML for the Google homepage. 
  }
})

This is just an example code and I will have my own, possibly mode code than this so I'm assuming putting this in odd place inside app.js is not considered best practice.

I'm also not sure if node_module folder is the right place for this executing purpose.

1
  • Advice on app structure depends entirely on the app's functionality, its size and continued development. I don't see how we'd be able to answer this. My hunch is that you should keep it in app.js until you feel that refactoring it into another file may make development easier. Commented Sep 29, 2015 at 10:47

1 Answer 1

1

As in my comment, it's difficult for the SO community to dictate how you structure your application without knowing a lot more about what the application does.

However, you have asked if node_modules would be a good place to execute your application code...

The answer to that is a resounding 'no'. The node_modules folder is specifically for modules that are used by the application, not the application itself.

A typical node application looks like this:

├── app.js
├── package.json
├── node_modules
   ├── module1
   ├── module2
   ├── etc...

Generally, your application would be written in app.js, and your external modules will be stored in node_modules by the Node Package Manager when executing the npm install command on the command line. NPM installs the packages that are listed in the package.json file. It would be bad practise to store application code in node_modules because this folder is often assumed to be re-buildable through NPM, and is usually removed by various applications/developers to make your application more transportable.

If you want to split your code into smaller chunks, then you can require specific files instead of known-modules.

Here's an example:

folder structure

├── app.js
├── lib
   ├── my-local-module.js
├── package.json
├── node_modules

lib/my-local-module.js

module.exports = function() {

  console.log('Hello World!');

}

app.js

var myLocalModule = require('./lib/my-local-module.js');

myLocalModule(); // Hello World!

Every program needs to start somewhere. In this case, you would bootstrap this application by running node app.js on the command line. That code, in turn, would localise your my-local-module.js file through the require statement.

As I said before; my advice would be to keep your code inside one file until you feel your codebase has gotten big enough to warrant refactoring and delegating logic to other files.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, it cleared up my doubts. My app only runs with npm start, I hope this won't affect localisation of my custom module?
@SeongLee So long as your package.json contains a start property in the scripts object, you should be fine. Something like: "start": "node app.js".
Thanks, this is probably a newbie question but how can I return the result of my module and assign to a variable in app.js? Calling myLocalModule(); won't allow me to use returned JSON data in my case.
@SeongLee Whatever you assign to module.exports in the module file will be returned from the require method call in your main application. So instead of assigning a function to module.exports, you could instead assign any valid JavaScript data type (objects, arrays, string numbers etc).
Can you please elaborate on this separate question? stackoverflow.com/questions/32844704/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.