简体   繁体   English

在本地主机上无法访问 React+Node+Express 应用程序:3000

[英]React+Node+Express app not accesible on localhost:3000

I have a web app with React as frontend and node+express as backend .我有一个 web 应用程序,以 React 作为frontend ,以 node+express 作为backend The folder structure of app is as in the pic below ( client folder is within the server folder)应用程序的文件夹结构如下图所示( client文件夹在server文件夹内) 在此处输入图像描述

The package.json in the client has the following code client中的package.json有如下代码

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

The package.json in the server has the following code server中的package.json有如下代码

"scripts": {
    "start": "node server.js",
    "server": "nodemon server.js",
    "client": "npm start --prefix client",
    "dev": "concurrently \"npm run server\" \"npm run client\""
  },

When I execute npm run dev , I get the message the my server is running on port 8080 and the react app is also compiled successfully and can be viewed on http://localhost:3000当我执行npm run dev时,我收到消息说我的服务器正在端口 8080 上运行并且反应应用程序也已成功编译并且可以在 http://localhost:3000 上查看在此处输入图像描述

But when I visit http://localhost:3000, i get the following但是当我访问 http://localhost:3000 时,我得到以下信息

在此处输入图像描述

It was working fine but when I added a button.module.css file, I got error from Typescript. One on the suggested solution on Stackoverflow was to create a declaration.d.ts file.它工作正常,但是当我添加一个button.module.css文件时,我从 Typescript 收到错误。Stackoverflow 上建议的解决方案之一是创建一个declaration.d.ts文件。 The error is gone now but the site is not loading !!!现在错误消失了,但是网站没有加载!!!

I don't how adding this file is the problem.我不知道添加这个文件是怎么回事。 I tried to run 'npm start' within the 'client` folder我尝试在“client”文件夹中运行“npm start”

在此处输入图像描述

But I do not know what happens, screen switches to 'npm run dev' within the 'server` folder但我不知道发生了什么,屏幕切换到“server”文件夹中的“npm run dev”

在此处输入图像描述

Can anyone help?谁能帮忙?

UPDATE更新

I created 2 more react app with npx create-react-app app --template typescript .我使用npx create-react-app app --template typescript创建了另外 2 个 React 应用程序。

  • One next to server folder and一个在server文件夹旁边,
  • one within server folder (next to client folder) server文件夹中的一个(在client文件夹旁边)

both of them worked just fine.他们两个都工作得很好。 No issue connecting to localhost:3000.连接到 localhost:3000 没有问题。

So I went comparing the contents one by one of my folder and the folders newly created.所以我去一个一个地比较我的文件夹和新创建的文件夹的内容。 As it turns out I had deleted the files named react-app-env.d.ts (this file is automatically created by npx create... ).事实证明我删除了名为react-app-env.d.ts的文件(该文件由 npx npx create...自动创建)。 I deleted because its content is just /// <reference types="react-scripts" /> and nothing else.我删除了,因为它的内容只是/// <reference types="react-scripts" /> ,没有别的。

I have seen a lot of tutorials where they deleted all the files of the src folder & I though this file was 'not useful'我看过很多教程,他们删除了src文件夹的所有文件,虽然这个文件“没用”

I have copied this file into my app folder & it works fine!!!我已将此文件复制到我的应用程序文件夹中并且工作正常!!!

Lesson Learnt = DO NOT TOUCH what you don't understand;经验教训= 不要碰你不明白的东西; Especially when things are working ;-)尤其是当一切正常时 ;-)

you want a multi-layer architecture but in think npm is working against you.您想要多层架构,但认为 npm 对您不利。

try unnnested projects, try single repo/multi project approach.尝试未嵌套的项目,尝试单一回购/多项目方法。

something like this: https://github.com/sombriks/palavrim/blob/main/package.json像这样的东西: https://github.com/sombriks/palavrim/blob/main/package.json

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

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