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

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.

主站蜘蛛池模板: 宣威市| 酒泉市| 田阳县| 成安县| 永宁县| 班玛县| 全椒县| 青铜峡市| 瑞丽市| 光泽县| 江华| 会同县| 新河县| 普宁市| 讷河市| 云阳县| 江川县| 汾西县| 丰都县| 永济市| 慈利县| 轮台县| 阿拉善盟| 淄博市| 小金县| 鄂尔多斯市| 衡东县| 白水县| 乌恰县| 博乐市| 丹棱县| 家居| 军事| 淳化县| 都兰县| 博白县| 富锦市| 大城县| 德令哈市| 建始县| 新乐市|