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

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.

主站蜘蛛池模板: 金平| 原阳县| 庆阳市| 林州市| 平远县| 武汉市| 石狮市| 盘山县| 安化县| 阿尔山市| 石屏县| 正蓝旗| 莱阳市| 四川省| 青海省| 蒲江县| 健康| 黄骅市| 靖江市| 西丰县| 沙湾县| 棋牌| 麦盖提县| 临湘市| 浠水县| 漳州市| 绥中县| 卫辉市| 博乐市| 太湖县| 微博| 四子王旗| 禄劝| 本溪市| 哈巴河县| 红桥区| 同江市| 连城县| 巴林右旗| 烟台市| 柳江县|