NodeJs 官方代码:
example.js http
var http = require(‘http‘); http.createServer(function (req, res) { res.writeHead(200, {‘Content-Type‘: ‘text/plain‘}); res.end(‘Hello World\n‘); }).listen(9337, ‘127.0.0.1‘); console.log(‘Server running at http://127.0.0.1:9337/‘);
example.js tcp
var net = require(‘net‘); var server = net.createServer(function (socket) { socket.write(‘Echo server\r\n‘); socket.pipe(socket); }); server.listen(9337, ‘127.0.0.1‘);
运行
node example.js
访问
http://127.0.0.1:9337/
如果部署在服务器,需要外网访问,把127.0.0.1改为对应服务器的IP地址,对应的防火墙端口开放出来 就好了
时间: 2024-10-09 23:13:49