我正在尝试让一个简单的react前端和nodejs后端启动并在vs代码中运行和调试。我使用compound启动配置来同时启动“客户端”和“服务器”。nodejs后台会自动为我启动,但我总是必须在控制台中执行npm start,才能让客户端进行连接。我看过的所有教程都表明,在vs代码中运行调试配置之前,必须执行此步骤。难道vs代码不能像nodejs后端一样启动前端吗?
这是我的launch.json的样子:
{
// 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"
}
]
}发布于 2019-01-11 20:16:03
客户端需要npm start的原因是因为您引用的是http://localhost:3000
npm start实际上将运行一个迷你web服务器来在http://localhost:3000上提供您的html文件
另一种方法是在您的src上使用类似于http-server的内容,这将具有相同的效果。
发布于 2019-03-19 06:01:36
我从一开始启动调试会话时就遇到了一些问题,所以我决定启动nodejs服务器,然后附加调试器。
我发现这个配置对我很有效。
{
"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中使用的一些脚本。
{
"scripts": {
"prestart": "rollup -c -w",
"start": "nodemon --ignore '*.ts' --inspect=9000 ./dist/server/index"
},
}我必须使用nodemon来检测更改并重新启动节点服务器。
但是,如果您的React应用程序需要与您的节点应用程序分开启动,那么我建议您使用类似http-server的东西来为您的React应用程序启动本地服务器。然后使用concurrently同时启动这两个应用程序。您的package.json可能如下所示:
{
"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
https://stackoverflow.com/questions/54146242
复制相似问题