3

I'm using GET and POST requests to apply RESTful conventions multiple times in my application. But this results in breaking my JSON-server connection. 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 ]
Knowledge Seeker
  • 526
  • 7
  • 25
  • Is there another instance running? What happens when you specify another port # like the error message prompts? – adamz4008 Dec 04 '19 at 06:02
  • @adamz4008 Yes. Ports `(3000, 3001, 3003)` are running at the same time. I'm getting this error only for `Port 3003` – Knowledge Seeker Dec 04 '19 at 06:06
  • @Knowledge Seeker Any specific reason you are trying to use port 3003? Can you try changing the port from 3003 to something else. I also had faced a similar issue earlier where the port 3003 was blocked and was not able to use it. So, I had to change the port and everything worked fine. I have no idea why was it blocked. I will try to find some relevant link and post it here. But for now try using 3000, 3001, 3002 or any other ports except 3003 and tell me if it works. – Sunil Chaudhary Dec 05 '19 at 20:58
  • @Sunil I'm afraid. I'm facing same error for port 3002. – Knowledge Seeker Dec 06 '19 at 04:45
  • did you get any solution ? – Muhammad Abdullah Apr 16 '20 at 15:19

3 Answers3

6

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. 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).

I was able to get rid of this error by specifying the --host 127.0.0.1 parameter when starting the json-server.

juanespty
  • 84
  • 1
  • 7
  • I was getting connection reset, just because imported db file to mock server for autoreload just didn't mention my connection was lost due to restart after mutation Your comment helped me to notice that :) – Elisey Oct 21 '22 at 11:36
0

I was also getting same error with 3000 port. I started my server on another port using below command, And my problem solved. json-server --watch db.json --port 3002

-1

That error means the port already being used, see json-server source:

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

Try killing the node process associated with that port and restarting, or otherwise specifying a new port through --port argument.

adamz4008
  • 660
  • 4
  • 10