欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Introduction to Node.js(1)

程序员文章站 2022-05-13 16:27:20
...

Introduction to Node.js(1)

1 Hello World

$ sudo port install nodejs
$ echo "console.log(\"Hello World\");" > helloword.js
$ node helloworld.js

2 A simple TCP server

Create a file named “simple_tcp_server.js”

require("net").createServer(function(socket) {
    socket.write('Echo server\r\n');
    socket.pipe(socket);
}).listen(8765, "127.0.0.1");

Run your server:

$ node simple_tcp_server.js

3 A simple HTTP server

Create a file named “simple_http_server.js”

require("http").createServer(function(request, response) {
    console.log("Hello World");
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World\n");
    response.end();
}).listen(8765, "127.0.0.1");

-

转载请注明来自柳大的CSDN博客:blog.csdn.net/poechant

-