[node.js] [3] Request the server and resolve port 3000


一、 server.listen()

server.listen(port, hostname, backlog, callback);
insert image description here

const http = require('http');
let counter = 0
const server = http.createServer((req,res) =>{
    
    
    res.end(`${
      
      ++counter}`)
})
server.listen(3000);
var http = require('http');
http.createServer(function (req, res) {
    
    
  res.writeHead(200, {
    
    'Content-Type': 'text/plain'});
  res.write('Hello  World!');
  res.end();
}).listen(3000 );
// 1. 引入模块
const http = require('http')
// 2. 创建服务器对象
const app = http.createServer()
// 3. 监听端口并启动服务器
app.listen(3000, () => {
    
    
    console.log('server is running at 127.0.0.1:3000')
})

Three callback methods

2. Solve the problem about port 3000

When the terminal displays

Error: listen EADDRINUSE: address already in use :::3000
indicates interface conflict, port 3000 is occupied

Solution:
netstat -aon|findstr "3000" //Find which programs occupy port 3000
tasklist|findstr 10932 //Release port 3000

insert image description here
When running a program, it is always easy to find that the port is occupied, and there are different solutions. If there is a better method, I hope you can point it out.

Guess you like

Origin blog.csdn.net/weixin_51612770/article/details/125023053