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

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.

主站蜘蛛池模板: 始兴县| 衡阳市| 彭泽县| 砀山县| 海城市| 巴青县| 邵阳县| 武川县| 阳江市| 康乐县| 南乐县| 涟源市| 霍州市| 资溪县| 柏乡县| 滨海县| 陆川县| 隆德县| 阿城市| 任丘市| 文水县| 博乐市| 通海县| 广灵县| 石楼县| 绩溪县| 巍山| 海门市| 稻城县| 如东县| 田林县| 新巴尔虎左旗| 西和县| 乌拉特后旗| 德兴市| 麦盖提县| 夏津县| 百色市| 孝感市| 政和县| 敖汉旗|