简体   繁体   English

JSON 服务器连接中断

[英]JSON-server connection breaks

I'm using GET and POST requests to apply RESTful conventions multiple times in my application.我正在使用GETPOST请求在我的应用程序中多次应用 RESTful 约定。 But this results in breaking my JSON-server connection.但这会导致我的JSON-server连接中断。 Does anyone know what is the reason behind this?有谁知道这背后的原因是什么? Any idea that why I'm facing this problem?知道为什么我面临这个问题吗?

Error that I'm getting is:我得到的错误是:

POST /points/ 201 10.344 ms - 289
POST /intersections/ 201 15.447 ms - 504
POST /points/ 201 7.159 ms - 307
POST /intersections/ 201 12.089 ms - 486
  db.json has changed, reloading...

  Loading db.json
  db.json has changed, reloading...
  Done

  Resources
  http://localhost:3003/streams
  http://localhost:3003/intersections
  http://localhost:3003/points

  Home
  http://localhost:3003

  db.json has changed, reloading...

  Loading db.json
  db.json has changed, reloading...
  Done

  Resources
  http://localhost:3003/streams
  http://localhost:3003/intersections
  http://localhost:3003/points

  Home
  http://localhost:3003

Cannot bind to the port 3003. Please specify another port number either through --port argument or through the json-server.json configuration file
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! api@1.0.0 start: `json-server -p 3003 -w db.json`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the api@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\knowledge.seeker\AppData\Roaming\npm-cache\_logs\2019-12-04T05_34_11_867Z-debug.log

My log file is:我的日志文件是:

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files (x86)\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'start' ]
2 info using npm@6.9.0
3 info using node@v10.16.3
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle api@1.0.0~prestart: api@1.0.0
6 info lifecycle api@1.0.0~start: api@1.0.0
7 verbose lifecycle api@1.0.0~start: unsafe-perm in lifecycle true
8 verbose lifecycle api@1.0.0~start: PATH: C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Users\knowledge.seeker\Desktop\ReactMapGL\api\node_modules\.bin;C:\Users\knowledge.seeker\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\local\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\knowledge.seeker\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files\Intel\WiFi\bin;C:\Program Files\Common Files\Intel\WirelessCommon;C:\Program Files (x86)\nodejs;C:\Program Files\Git\cmd;C:\Program Files (x86)\Yarn\bin;C:\Users\knowledge.seeker\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Intel\WiFi\bin;C:\Program Files\Common Files\Intel\WirelessCommon;C:\Users\knowledge.seeker\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\knwoledge.seeker\AppData\Local\Yarn\bin;C:\Program Files\Git\usr\bin\vendor_perl;C:\Program Files\Git\usr\bin\core_perl
9 verbose lifecycle api@1.0.0~start: CWD: C:\Users\knowledge.seeker\Desktop\ReactMapGL\api
10 silly lifecycle api@1.0.0~start: Args: [ '/d /s /c', 'json-server -p 3003 -w db.json' ]
11 silly lifecycle api@1.0.0~start: Returned: code: 1  signal: null
12 info lifecycle api@1.0.0~start: Failed to exec start script
13 verbose stack Error: api@1.0.0 start: `json-server -p 3003 -w db.json`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:301:16)
13 verbose stack     at EventEmitter.emit (events.js:198:13)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files (x86)\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:198:13)
13 verbose stack     at maybeClose (internal/child_process.js:982:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
14 verbose pkgid api@1.0.0
15 verbose cwd C:\Users\knowledge.seeker\Desktop\ReactMapGL\api
16 verbose Windows_NT 10.0.17763
17 verbose argv "C:\\Program Files (x86)\\nodejs\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start"
18 verbose node v10.16.3
19 verbose npm  v6.9.0
20 error code ELIFECYCLE
21 error errno 1
22 error api@1.0.0 start: `json-server -p 3003 -w db.json`
22 error Exit status 1
23 error Failed at the api@1.0.0 start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

I was getting the same error in my development environment, when changes to the db.json file were made and the json-server automatically reloaded.当对 db.json 文件进行更改并且 json-server 自动重新加载时,我在我的开发环境中遇到了同样的错误。 Sometimes it would come back with the "port already in use" error, regardless of which port I would specify (100% certain it was not used by any other application).有时它会返回“端口已在使用中”错误,无论我指定哪个端口(100% 确定它没有被任何其他应用程序使用)。

I was able to get rid of this error by specifying the --host 127.0.0.1 parameter when starting the json-server.通过在启动 json-server 时指定--host 127.0.0.1参数,我能够摆脱这个错误。

I was also getting same error with 3000 port.我在 3000 端口上也遇到了同样的错误。 I started my server on another port using below command, And my problem solved.我使用以下命令在另一个端口上启动了我的服务器,我的问题解决了。 json-server --watch db.json --port 3002 json-server --watch db.json --port 3002

That error means the port already being used, see json-server source:该错误表示该端口已被使用,请参阅 json-server 源:

process.on('uncaughtException', error => {
        if (error.errno === 'EADDRINUSE')
          console.log(
            chalk.red(
              `Cannot bind to the port ${error.port}. Please specify another port number either through --port argument or through the json-server.json configuration file`
            )
          )
        else console.log('Some error occurred', error)
        process.exit(1)
      })

https://github.com/typicode/json-server/blob/4f315b090f6a504c261bab83894d3eb85eabe686/src/cli/run.js#L138 https://github.com/typicode/json-server/blob/4f315b090f6a504c261bab83894d3eb85eabe686/src/cli/run.js#L138

Try killing the node process associated with that port and restarting, or otherwise specifying a new port through --port argument.尝试终止与该端口关联的节点进程并重新启动,或者通过 --port 参数以其他方式指定新端口。

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

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