javascript在我的客户端和服务器文件夹之间共享代码 [英] javascript Share code between my client and my server folders

查看:78
本文介绍了javascript在我的客户端和服务器文件夹之间共享代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个文件夹的目录:我的服务器应用程序( NodeJS )和我的客户端应用程序( ReactJS )。

I have a directory that contains two folder : my server app (NodeJS) and my client app (ReactJS).

我的目录结构如下:

root/
|- client/
   |- package.json
   |- src/
      |- ...
|- server/
   |- package.json
   |- src/
      |- ...

我如何在目录的根目录中添加 common 文件夹,以便与服务器和客户端共享枚举和实用程序?

How can I add a common folder at the root of my directory to share enums and utils with both my server and my client ?

谢谢!

推荐答案

您有多种解决方案:


  • 在根目录下创建一个常用文件夹,只需要从文件中获取所需的文件即可。但是您最终可能会遇到长要求,例如require( ../../../../../ common / file)

  • create a folder common under root and just require the files you need from your files. But you could end up with "long" require such as require("../../../../../common/file")

使用module-alias避免该问题: https://github.com/ilearnio/module-别名

use module-alias to avoid that problem: https://github.com/ilearnio/module-alias

您可以将 common 设为本地模块(使用文件:)并将其安装在package.json
https://docs.npmjs.com/files/package.json#local-paths

you could make common a local module (using file:) and install it in package.json https://docs.npmjs.com/files/package.json#local-paths

{
  "name": "common",
  "dependencies": {
    "common": "file:../common"
  }
}

然后,您只需轻松地需要您所需要的

Then you can just require what you need easily

这篇关于javascript在我的客户端和服务器文件夹之间共享代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆