Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 指定主机名时在node.js中获取EADDRINUSE错误_Javascript_Node.js_Express_Localhost - Fatal编程技术网

Javascript 指定主机名时在node.js中获取EADDRINUSE错误

Javascript 指定主机名时在node.js中获取EADDRINUSE错误,javascript,node.js,express,localhost,Javascript,Node.js,Express,Localhost,尝试运行此代码时,我在nodejs上遇到EADDRINUSE错误: const express = require('express') const http = require('http') const hostname = 'localhost' const port = 3000 const app = express() app.use((req, res, next) => { console.log(req.headers) res.statusC

尝试运行此代码时,我在nodejs上遇到EADDRINUSE错误:

const express = require('express')
const http = require('http')    
const hostname = 'localhost'
const port = 3000

const app = express()

app.use((req, res, next) => {
    console.log(req.headers)
    res.statusCode = 200
    res.setHeader('Content-Type', 'text/html')
    res.end('<html><body><h1>This is response</h1></body></html>')
})

server.listen(hostname,port, () => {
    console.log(`Server running at http:${hostname}//:${port}`)
})

const server = http.createServer(app)

server.listen(hostname,port, () => {
    console.log(`Server running at http:{}//:${port}`)
})
const express=require('express'))
const http=require('http')
常量主机名='localhost'
常数端口=3000
const app=express()
应用程序使用((请求、恢复、下一步)=>{
控制台日志(请求头)
res.statusCode=200
res.setHeader('Content-Type','text/html')
res.end('这是响应')
})
侦听(主机名,端口,()=>{
log(`Server running at http:${hostname}/:${port}`)
})
const server=http.createServer(应用程序)
侦听(主机名,端口,()=>{
log(`Server running at http:{}/:${port}`)
})
我尝试更改端口,但仍然出现相同的错误。 特别的是,如果我删除主机名--“localhost”,并且只监听端口,代码就会工作

请解释可能的问题是什么

以下是堆栈跟踪:

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE localhost
    at Object._errnoException (util.js:1022:11)
    at _exceptionWithHostPort (util.js:1044:20)
    at Server.setupListenHandle [as _listen2] (net.js:1350:19)
    at listenInCluster (net.js:1408:12)
    at Server.listen (net.js:1503:5)
    at Object.<anonymous> (/media/shikhar/D/Study/git-test/node-express/index.js:30:8)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
events.js:183
投掷者;//未处理的“错误”事件
^
错误:使用本地主机侦听
对象处的错误异常(util.js:1022:11)
at_例外WithHostPort(util.js:1044:20)
在Server.setupListenHandle[as _listen2](net.js:1350:19)
在ListeniCluster(net.js:1408:12)
在Server.listen(net.js:1503:5)
反对。(/media/shikhar/D/Study/git-test/node-express/index.js:30:8)
编译(Module.js:652:30)
在Object.Module.\u extensions..js(Module.js:663:10)
在Module.load(Module.js:565:32)
在tryModuleLoad时(module.js:505:12)

您是否在代理后?请尝试仅使用
应用程序。侦听(端口、回调)
无代理后。当我传递主机名时,app.listen给出了相同的错误