首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于启动调试前端和后端的VS代码设置

用于启动调试前端和后端的VS代码设置
EN

Stack Overflow用户
提问于 2019-01-11 20:06:08
回答 2查看 3.2K关注 0票数 4

我正在尝试让一个简单的react前端和nodejs后端启动并在vs代码中运行和调试。我使用compound启动配置来同时启动“客户端”和“服务器”。nodejs后台会自动为我启动,但我总是必须在控制台中执行npm start,才能让客户端进行连接。我看过的所有教程都表明,在vs代码中运行调试配置之前,必须执行此步骤。难道vs代码不能像nodejs后端一样启动前端吗?

这是我的launch.json的样子:

代码语言:javascript
复制
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"compounds": [
    {
        "name": "Client+Server",
        "configurations": [ "Server", "Client" ]
    }
],
"configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Server",
        "program": "${workspaceFolder}/server/server.js"
    },
    {
        "type": "chrome",
        "request": "launch",
        "name": "Client",
        "url": "http://localhost:3000",
        "webRoot": "${workspaceFolder}/client/src/index.js"
    }
]
}
EN

回答 2

Stack Overflow用户

发布于 2019-01-11 20:16:03

客户端需要npm start的原因是因为您引用的是http://localhost:3000

npm start实际上将运行一个迷你web服务器来在http://localhost:3000上提供您的html文件

另一种方法是在您的src上使用类似于http-server的内容,这将具有相同的效果。

票数 0
EN

Stack Overflow用户

发布于 2019-03-19 06:01:36

我从一开始启动调试会话时就遇到了一些问题,所以我决定启动nodejs服务器,然后附加调试器。

我发现这个配置对我很有效。

代码语言:javascript
复制
{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "attach",
      "name": "Attach Server",
      "restart": true,
      "port": 9000
    }, {
      "type": "chrome",
      "request": "launch",
      "name": "Launch Client",
      "port": 9001,
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}/dist",
      "sourceMaps": true
    }
  ],
  "compounds": [
    {
      "name": "Attach Client+Server",
      "configurations": ["Attach Server", "Launch Client"]
    }
  ]
}

下面是我在package.json中使用的一些脚本。

代码语言:javascript
复制
{
  "scripts": {
    "prestart": "rollup -c -w",
    "start": "nodemon --ignore '*.ts' --inspect=9000 ./dist/server/index"
  },
}

我必须使用nodemon来检测更改并重新启动节点服务器。

但是,如果您的React应用程序需要与您的节点应用程序分开启动,那么我建议您使用类似http-server的东西来为您的React应用程序启动本地服务器。然后使用concurrently同时启动这两个应用程序。您的package.json可能如下所示:

代码语言:javascript
复制
{
  "scripts": {
    "prestart": "rollup -c -w",
    "start:client": "http-server ./dist/client/",
    "start:server": "nodemon --ignore '*.ts' --inspect=9000 ./dist/server/index",
    "serve": "concurrently \"npm:start:client\" \"npm:start:server\""
  },
}

资源VS代码调试配置:https://code.visualstudio.com/Docs/editor/debugging

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54146242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档