[英]How can I structure an Express+React TypeScript project well?
I am currently trying to set up a project that uses Express for the backend and React for the frontend.我目前正在尝试建立一个使用 Express 作为后端并使用 React 作为前端的项目。 I also want to use TypeScript for both parts.
我还想对这两个部分使用 TypeScript。 For easy setup, I used
npx create-react-app client --template typescript
and npx express-generator-typescript server
, resulting in the following directory structure:为了方便设置,我使用
npx create-react-app client --template typescript
和 npx npx express-generator-typescript server
,得到以下目录结构:
myProjectRoot/
|_ server/
|_ src/
|_ node_modules/
|_ package.json
|_ tsconfig.json
|_ ...
|_ client/
|_ src/
|_ node_modules/
|_ package.json
|_ tsconfig.json
|_ ...
Now I configured React's development server in client/package.json
with a line "proxy": "http://localhost:3001"
and let the Express server listen on port 3001
to handle API requests.现在,我在
client/package.json
中配置了 React 的开发服务器,其中包含一行"proxy": "http://localhost:3001"
,并让 Express 服务器侦听端口3001
以处理 API 请求。 So far, so good;到目前为止,一切都很好; for development, this is perfectly fine imho.
对于发展,这是非常好的恕我直言。
However, I am a bit confused how to best set up the commands for a production build.但是,我有点困惑如何最好地设置生产构建的命令。 I wanted to change the default-configured build directories to avoid having client and server build each in an isolated directory:
我想更改默认配置的构建目录,以避免让客户端和服务器分别在一个独立的目录中构建:
myProjectRoot/
|_ server/
|_ ...
|_ src/
|_ dist/
|_ compiled_backend_code
|_ client/
|_ ...
|_ src/
|_ build/
|_ compiled_frontend_code
Instead, I aimed for something like this because it seems much cleaner to me:相反,我的目标是这样的,因为它对我来说似乎更干净:
myProjectRoot/
|_ server/
|_ client/
|_ dist/
|_ server/
|_ compiled_backend_code
|_ client
|_ compiled_frontend_code
To get there, I changed the outDir
of the server's tsconfig.json
to ../dist/server
and the build
script in the client's package.json
to react-scripts build && mv build../dist/client
(as explained here ). To get there, I changed the
outDir
of the server's tsconfig.json
to ../dist/server
and the build
script in the client's package.json
to react-scripts build && mv build../dist/client
(as explained here ) .
The problem with this now is that the compiled code does not find its dependencies any more because the server's and client's node_modules
folders are not located in the dist/
directory or a parent directory any more.现在的问题是编译后的代码不再找到它的依赖项,因为服务器和客户端的
node_modules
文件夹不再位于dist/
目录或父目录中。 I'm not sure what a good solution to this problem is, so I'd appreciate any input, even if it means a completely different project structure.我不确定这个问题有什么好的解决方案,所以我很感激任何意见,即使这意味着完全不同的项目结构。 Thanks!
谢谢!
You can achieve this in two ways that I know of:你可以通过我知道的两种方式来实现这一点:
Keeping client build folder inside client root, and then addressing it like in this example .将客户端构建文件夹保留在客户端根目录中,然后像本示例中一样对其进行处理。
Use Yarn Workspaces , where node_modules
are mainly in the root folder (except for when packages are using different versions of the same dependency)使用Yarn Workspaces ,其中
node_modules
主要位于根文件夹中(包使用相同依赖项的不同版本时除外)
BTW, this repo is a boilerplate I've created for a full stack Express + React Web application in Typescript using Yarn Workspaces .顺便说一句,这个 repo 是我使用Yarn Workspaces为 Typescript 中的全栈 Express + React Web 应用程序创建的样板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.