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

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.

主站蜘蛛池模板: 马关县| 平原县| 锡林郭勒盟| 西城区| 托克托县| 姜堰市| 张家界市| 武汉市| 安达市| 宝坻区| 财经| 宝坻区| 尖扎县| 阳信县| 阜阳市| 高清| 阜宁县| 洛浦县| 内乡县| 西丰县| 吉木乃县| 类乌齐县| 岚皋县| 正镶白旗| 鄂州市| 梅河口市| 浠水县| 托克托县| 紫金县| 保定市| 余江县| 西乌| 澄迈县| 武邑县| 庄河市| 新蔡县| 红安县| 五指山市| 康定县| 泸州市| 大荔县|