简体   繁体   English

文件结构的最佳做法-使用请求模块(Node.js + Express.js)

[英]Best practice on file structure - using Request module (Node.js + Express.js)

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. 我刚刚在express + node.js项目中安装了Request模块,我想知道我应该在哪个文件夹中放置自定义js文件,该文件将包含以下基本执行Request模块的代码。

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. 这只是一个示例代码,我将拥有自己的代码(可能是模式代码),因此我假设将其放在app.js中的奇怪位置不被视为最佳实践。

I'm also not sure if node_module folder is the right place for this executing purpose. 我也不确定node_module文件夹是否适合此执行目的。

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. 正如我的评论所述,SO社区很难在不了解应用程序功能的情况下就决定如何构造应用程序。

However, you have asked if node_modules would be a good place to execute your application code... 但是,您问过node_modules是否是执行应用程序代码的好地方...

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. node_modules文件夹专门用于应用程序使用的模块,而不是应用程序本身。

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. 通常,您的应用程序将用app.js编写,并且在命令行上执行npm install命令时, 节点软件包管理器会将外部模块存储在node_modules NPM installs the packages that are listed in the package.json file. NPM将安装package.json文件中列出的软件包。 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. 将应用程序代码存储在node_modules中将是一个坏习惯,因为通常假定此文件夹可以通过NPM重新构建,并且通常被各种应用程序/开发人员删除,以使您的应用程序更具可移植性。

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 lib / my-local-module.js

module.exports = function() {

  console.log('Hello World!');

}

app.js 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. 在这种情况下,您可以通过在命令行上运行node app.js来引导该应用程序。 That code, in turn, would localise your my-local-module.js file through the require statement. 相应地,该代码将通过require语句对my-local-module.js文件进行本地化。

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. 我的建议是将代码保留在一个文件中,直到您觉得自己的代码库足够大以保证将逻辑重构和委派给其他文件为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM