首页 > 解决方案 > 后端和前端的文件夹结构

问题描述

我建立了一个带有 express node.js 后端和反应前端的项目。这是我第一次设置一个带有后端的项目,它们是我不确定的一些事情......

第一个问题:

所以我当前的文件夹结构是这样的: --

backend --node_modules
   --package
   -lock.json
   -- package.json
   --server.js
   --yarn.lock
--client
   --node_modules
   --package.json
   --public
   --.gitignore
   --README.md
   --yarn.lock
   --src
      --boilerplate create-react -app 文件

我的 package.json 文件:
BACKEND

{
  "name": "yelp-clone-2-backend",
  "license": "MIT",
  "version": "1.0.0",
  "scripts": {
    "client": "cd client && yarn start",
    "server": "nodemon server.js",
    "dev": "concurrently --kill-others-on-fail \"yarn server\" \"yarn client\""
  },
  "dependencies": {
    "body-parser": "^1.18.3",
    "express": "^4.17.1"
  },
  "devDependencies": {
    "concurrently": "^4.0.1"
  }
}

我的 package.json 文件:

前端

{
  "name": "yelp-clone-2-front-end",
  "version": "0.1.0",
  "license": "MIT",
  "private": true,
  "proxy": "http://localhost:5000/",
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}


我正在使用来自 BACKEND package.json 的命令来组合前端和后端服务器
"dev": "concurrently --kill-others-on-fail \"yarn server\" \"yarn client\""

当我运行这个命令(从 /backend 目录)时,我遇到的问题是我当前的文件夹结构

[1] /bin/sh: line 0: cd: client: No such file or directory
error Command failed with exit code 1.

但是...如果我将所有内容移出后端并移入根目录,因此在客户端之外而不是在后端文件夹中,则该命令将起作用,并且服务器将启动并像预期的那样在端口 5000 上侦听。

为什么该命令仅适用于根目录中的后端文件,而不适用于我想要的后端文件夹。

在启动服务器之前,我尝试在后端文件夹中运行以下命令,但没有成功:
rm -rf node_modules
yarn cache clean
yarn
yarn start

标签: node.jsreactjsexpressdirectory-structuregit-init

解决方案


  • 对于无法运行的 cd 命令,您需要了解 npm 命令是在后端文件夹中执行的。这意味着如果要将目录更改为客户端文件夹,则需要在文件夹前附加两个点:cd ../client. 您试图转到不存在的后端/客户端。

  • 要生成 git 存储库,您需要运行git init而不是npm init.

请在盲目使用之前了解 cd 命令的工作原理,因为它在专业环境中可能会产生一些非常糟糕的结果。

如有更多问题,请回复此答案,我很乐意对其进行编辑。


推荐阅读