我刚开始学习 nodejs 服务器和 websockets。 说我有这个服务器使用 socket.io 和 express 用 javascript 编写。
var app = require('express')(),
server = require('http').Server(app),
io = require('socket.io')(server),
port = process.env.PORT || 8080;
有什么区别:
server.listen(port, function(){
console.log("listening port " + port);
});
和
server.listen(port);
console.log("listening port " + port);
显然它们的工作原理相同。
那么 server.listen() 到底做了什么?
最佳答案
This function is asynchronous. When the server has been bound, 'listening' event will be emitted.
它使用回调,因为回调中的日志语句是对端口已绑定(bind)的确认。
Apparently they work the same.
不正确。如果您在回调之外登录,当然,它仍会记录端口号,但这与端口的实际边界同时发生,您不知道它是否成功。
关于javascript - 关于 nodejs server.listen(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35368867/