官术网_书友最值得收藏!

A simple server with Node.js

To see an example of how lightweight Node.js can be, let's take a look at some sample code that starts up an HTTP server and sends Hello World to a browser:

var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080');

A few basic lines of code are all it takes to write a complete Node.js application. Running it with a simple Node.js app.js command will launch an HTTP server that is listening on port 8080. Point any browser to http://localhost:8080, and you will see the simple output Hello World on your screen! While this sample app doesn't actually do anything useful, it should give you a glimpse of the kind of power you will have while writing web applications using Node.js. If you don't have the initial Node.js development environment set up, we will discuss it in the next chapter.

主站蜘蛛池模板: 绥芬河市| 西丰县| 常熟市| 潜江市| 富源县| 观塘区| 台东县| 富锦市| 洞口县| 榕江县| 达日县| 延安市| 镇安县| 民乐县| 前郭尔| 克拉玛依市| 泰兴市| 九台市| 延吉市| 昌邑市| 大石桥市| 林甸县| 洛阳市| 望江县| 宁河县| 广南县| 栾城县| 曲松县| 宜都市| 壶关县| 商洛市| 祁门县| 普兰县| 霍州市| 江门市| 张北县| 林州市| 莒南县| 峨山| 江永县| 台北市|