你想在网络上提供一个服务器。
创建一个基本的TCP服务器。
net = require 'net'
domain = 'localhost'
port = 9001
server = net.createServer (socket) ->
console.log "Received connection from #{socket.remoteAddress}"
socket.write "Hello, World!\n"
socket.end()
console.log "Listening to #{domain}:#{port}"
server.listen port, domain
可访问Basic Client:
$ coffee basic-server.coffee
Listening to localhost:9001
Received connection from 127.0.0.1
Received connection from 127.0.0.1
[...]
函数将为每个客户端新连接的新插口传递给@net.createServer@ 。基本的服务器与访客只进行简单地交互,但是复杂的服务器会将插口连上一个专用的处理程序,然后返回等待下一个用户的任务。
另请参阅Basic Client,Bi-Directional Server和Bi-Directional Client。