![](/img/loading.gif)
TCP是传输层的,所以相当于通过传输层实现应用层服务
使用NodeJS原生的net模块,就可以打通TCP传输层,且其提供一个端口号进行监听
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import net from 'net'
const html = `<h1>Hello World</h1>`
const headers = [ 'HTTP/1.1 200 OK', 'Content-Type: text/html', `Content-Length: ${html.length}`, 'Date: Wed, 9 Sep 2009 09:09:09 GMT', `\r\n`, html ]
const server = net.createServer((socket)=> { socket.on('data', (data)=> { if(/GET/.test(data.toString())) { socket.write(headers.join('\r\n')) socket.end() } }) })
server.listen(11451, ()=> { console.log('server is running!', server.address()) })
|
确实能被浏览器识别
![](/img/loading.gif)