使用node的http模塊
用Node.js實(shí)現(xiàn)一個(gè)HTTP服務(wù)器程序非常簡(jiǎn)單熙宇。我們來(lái)實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的Web程序hello.js鳖擒,它對(duì)于所有請(qǐng)求,都返回Hello world!:
'use strict';
// 導(dǎo)入http模塊:
var http = require('http');
// 創(chuàng)建http server烫止,并傳入回調(diào)函數(shù):
var server = http.createServer(function (request, response) {
// 回調(diào)函數(shù)接收request和response對(duì)象,
// 獲得HTTP請(qǐng)求的method和url:
console.log(request.method + ': ' + request.url);
// 將HTTP響應(yīng)200寫(xiě)入response, 同時(shí)設(shè)置Content-Type: text/html:
response.writeHead(200, {'Content-Type': 'text/html'});
// 將HTTP響應(yīng)的HTML內(nèi)容寫(xiě)入response:
response.end('<h1>Hello world!</h1>');
});
// 讓服務(wù)器監(jiān)聽(tīng)8080端口:
server.listen(8080);
console.log('Server is running at http://127.0.0.1:8080/');
//在命令提示符下運(yùn)行該程序蒋荚,可以看到//以下輸出:
//$ node hello.js
//Server is running at http://127.0.0.1:8080/
不要關(guān)閉命令提示符,直接打開(kāi)瀏覽器輸入http://localhost:8080馆蠕,即可看到服務(wù)器響應(yīng)的內(nèi)容:
http-hello-sample
同時(shí)期升,在命令提示符窗口惊奇,可以看到程序打印的請(qǐng)求信息:
GET: /
GET: /favicon.ico
當(dāng)然但我們要擴(kuò)展我們的node服務(wù)器,做成一個(gè)文件服務(wù)器吓妆。我們需要解析==request.url==中的路徑赊时。
現(xiàn)在我們要用到node的url,path模塊
最后行拢,我們實(shí)現(xiàn)一個(gè)文件服務(wù)器file_server.js:
'use strict';
var
fs = require('fs'),
url = require('url'),
path = require('path'),
http = require('http');
// 從命令行參數(shù)獲取root目錄祖秒,默認(rèn)是當(dāng)前目錄:
var root = path.resolve(process.argv[2] || '.');
console.log('Static root dir: ' + root);
// 創(chuàng)建服務(wù)器:
var server = http.createServer(function (request, response) {
// 獲得URL的path,類似 '/css/bootstrap.css':
var pathname = url.parse(request.url).pathname;
// 獲得對(duì)應(yīng)的本地文件路徑舟奠,類似 '/srv/www/css/bootstrap.css':
var filepath = path.join(root, pathname);
// 獲取文件狀態(tài):
fs.stat(filepath, function (err, stats) {
if (!err && stats.isFile()) {
// 沒(méi)有出錯(cuò)并且文件存在:
console.log('200 ' + request.url);
// 發(fā)送200響應(yīng):
response.writeHead(200);
// 將文件流導(dǎo)向response:
fs.createReadStream(filepath).pipe(response);
} else {
// 出錯(cuò)了或者文件不存在:
console.log('404 ' + request.url);
// 發(fā)送404響應(yīng):
response.writeHead(404);
response.end('404 Not Found');
}
});
});
server.listen(8080);
console.log('Server is running at http://127.0.0.1:8080/');
問(wèn)題詳情
我們想測(cè)試一下node 的http模塊
'use strict';
// 導(dǎo)入http模塊:
var http = require('http');
// 創(chuàng)建http server竭缝,并傳入回調(diào)函數(shù):
var server = http.createServer(function(request, response) {
// 回調(diào)函數(shù)接收request和response對(duì)象,
// 獲得HTTP請(qǐng)求的method和url:
console.log(request.method + ': ' + request.url);
// 將HTTP響應(yīng)200寫(xiě)入response, 同時(shí)設(shè)置Content-Type: text/html:
response.writeHead(200, { 'Content-Type': 'text/html' });
// 將HTTP響應(yīng)的HTML內(nèi)容寫(xiě)入response:
response.end('<h1>Hello world!</h1>');
});
// 讓服務(wù)器監(jiān)聽(tīng)8080端口:
server.listen(8080);
console.log('Server is running at http://127.0.0.1:8080/');
我遇到一些問(wèn)題,cmd報(bào)錯(cuò)了
Server is running at http://127.0.0.1:8080/
GET: /__webpack_hmr
GET: /__webpack_hmr
GET: /__webpack_hmr
GET: /__webpack_hmr
GET: /__webpack_hmr
//hmr就是hot module replacement
//模塊熱替換沼瘫,這也就是為什么你不刷新頁(yè)面就可以更
//新視圖的原因抬纸,所以,肯定是一直請(qǐng)求的
最后發(fā)現(xiàn)是最常用的端口8080被占用了耿戚,然后我們把
server.listen(8000);
//然后在瀏覽器中打開(kāi)http://127.0.0.1:8000就可以了