Node.js 是一個(gè)基于 Chrome V8 引擎的 JavaScript 運(yùn)行環(huán)境。 Node.js 使用了一個(gè)事件驅(qū)動(dòng)、非阻塞式 I/O 的模型,使其輕量又高效。Node.js 的包管理器 npm,是全球最大的開(kāi)源庫(kù)生態(tài)系統(tǒng)。(nodejs官網(wǎng)上的介紹),正如官網(wǎng)上介紹的那樣,nodejs確實(shí)很牛!怎么個(gè)牛法?看看下面的代碼就知道了。 //引入http模塊
var http = require("http");
//設(shè)置主機(jī)名
var hostName = '127.0.0.1';
//設(shè)置端口
var port = 8080;
//創(chuàng)建服務(wù)
var server = http.createServer(function(req,res){
res.setHeader('Content-Type','text/plain');
res.end("hello nodejs");
});
server.listen(port,hostName,function(){
console.log(`服務(wù)器運(yùn)行在http://${hostName}:${port}`);
});
短短幾行代碼就把一個(gè)簡(jiǎn)單的web服務(wù)器搭建完成了,為了驗(yàn)證效果,我們?cè)跒g覽器請(qǐng)求,結(jié)果如下: 
運(yùn)行成功! 到此為止,一個(gè)web服務(wù)器就建立成功了! 沒(méi)錯(cuò)就是這么簡(jiǎn)單,然后我們就可以寫(xiě)個(gè)html界面愉快的玩耍了,哈哈哈!果斷的寫(xiě)了一個(gè)html頁(yè)面來(lái)請(qǐng)求一下我們的web服務(wù)器。 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="jquery-1.2.1.js"></script>
</head>
<body>
<div class="container">
<div class="btn" onclick="getText()">獲取數(shù)據(jù)</div>
<div class="text"></div>
</div>
</body>
<script type="text/javascript">
function getText(){
$(".text").load("http:127.0.0.1:8080");
}
</script>
</html>
代碼簡(jiǎn)單,點(diǎn)擊div獲取數(shù)據(jù)并將服務(wù)器返回的數(shù)據(jù)展示。好了,我們運(yùn)行一下demo.html文件,我擦來(lái)!居然出現(xiàn)了…… 
很明顯,通過(guò)jquery請(qǐng)求不到數(shù)據(jù),這是因?yàn)榭缬蛘?qǐng)求的原因。我們的web服務(wù)器并不支持跨域請(qǐng)求,所以報(bào)錯(cuò)了。解決方式:在服務(wù)器的響應(yīng)頭文件里加上如下代碼: res.setHeader('Content-Type','text/plain');
res.setHeader('Access-Control-Allow-Origin',"*")
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.end("hello nodejs");
再次重啟服務(wù)器,運(yùn)行demo.html,顯示結(jié)果很是令人欣喜! 
通常請(qǐng)求服務(wù)器都會(huì)拼接參數(shù)的,最常用的就是get請(qǐng)求,post請(qǐng)求。很明顯,我們現(xiàn)在的代碼還不能支持。express框架很好的封裝了nodejs的http模塊,使我們用起來(lái)非常的簡(jiǎn)單。 引入express :$ cnpm install express –save var express = require("express");
var app = express();
var hostName = '127.0.0.1';
var port = 8080;
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
app.get("/get",function(req,res){
console.log("請(qǐng)求url:",req.path)
console.log("請(qǐng)求參數(shù):",req.query)
res.send("這是get請(qǐng)求");
})
app.listen(port,hostName,function(){
console.log(`服務(wù)器運(yùn)行在http://${hostName}:${port}`);
});
使用方式變化不大,通過(guò)express()方法開(kāi)啟服務(wù),然后在通過(guò)get方法來(lái)設(shè)置匹配參數(shù)的路由,通過(guò)在回調(diào)函數(shù)的req中可以獲取請(qǐng)求參數(shù)和地址。post請(qǐng)求也是類似,不過(guò)有不同的是,post請(qǐng)求在獲取參數(shù)的時(shí)候要引入body-parser 中間件,用于處理 JSON, Raw, Text 和 URL 編碼的數(shù)據(jù)。 var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
var hostName = '127.0.0.1';
var port = 8080;
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By",' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
app.get("/get",function(req,res){
console.log("請(qǐng)求url:",req.path)
console.log("請(qǐng)求參數(shù):",req.query)
res.send("這是get請(qǐng)求");
})
app.post("/post",function(req,res){
console.log("請(qǐng)求參數(shù):",req.body);
var result = {code:200,msg:"post請(qǐng)求成功"};
res.send(result);
});
app.listen(port,hostName,function(){
console.log(`服務(wù)器運(yùn)行在http://${hostName}:${port}`);
});
運(yùn)行結(jié)果: 
完整的get以及post請(qǐng)求就是以上了。下一篇博客會(huì)結(jié)果fs文件模塊介紹http是如何返回文件的,敬請(qǐng)期待!
|