當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
NodeJS Web模块
生活随笔
收集整理的這篇文章主要介紹了
NodeJS Web模块
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
NodeJS Web模塊
本文介紹nodeJS的http模塊的基本用法,實(shí)現(xiàn)簡(jiǎn)單服務(wù)器和客戶端
經(jīng)典Web架構(gòu)
- Client:客戶端一般指瀏覽器,通過HTTP協(xié)議向服務(wù)器發(fā)送請(qǐng)求(request)
- Server:服務(wù)器,接受客戶端請(qǐng)求,并向服務(wù)器發(fā)送響應(yīng)數(shù)據(jù)(response),主流的有Apache、Nginx、IIS
- Business:業(yè)務(wù)邏輯層,核心應(yīng)用邏輯所在,與數(shù)據(jù)庫(kù)、文件系統(tǒng)、外部程序交互
- Data:數(shù)據(jù)層,主要由數(shù)據(jù)庫(kù)組成
Node服務(wù)器
server.js
var http = require('http') var fs = require('fs') var url = require('url') // 創(chuàng)建服務(wù)器 http.createServer(function (request, response) {// 解析請(qǐng)求,包括文件名var pathname = url.parse(request.url).pathname// 日志輸出請(qǐng)求的文件名console.log("Request for "+pathname+" received.")// 從文件系統(tǒng)中讀取請(qǐng)求的內(nèi)容fs.readFile(pathname.substr(1),function (err, data) {if (err){console.error(err)//返回錯(cuò)誤信息404response.writeHead(404,{"Content-Type":"text/html"})}else{// 請(qǐng)求成功response.writeHead(200,{"Content-Type":"text/html"})response.write(data.toString())}response.end()}) }).listen(8000) //在8000端口監(jiān)聽console.log("Server is running at http://127.0.0.1:8000")在同一目錄下新建index.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Sample Page</title> </head> <body> <h1>Hello World</h1> </body> </html>運(yùn)行server.js之后,在瀏覽器中訪問http://127.0.0.1:8000/index.html,就會(huì)返回helloworld的頁(yè)面
Node客戶端
client.js
var http = require('http') // 配置參數(shù) var options = {host:'localhost',port:'8000',path:'/index.html' }// 向服務(wù)器發(fā)送請(qǐng)求 var req = http.request(options, function (response) {var body = ''// 接受數(shù)據(jù)塊response.on('data',function (data) {body += data})response.on('end',function () {console.log(body)}) }) req.end()服務(wù)器在運(yùn)行的同時(shí),再開一個(gè)終端
$ node client.js輸出為:
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Sample Page</title> </head> <body> <h1>Hello World</h1> </body> </html>GET請(qǐng)求
var http = require('http') var url = require('url')http.createServer(function (req, res) {res.writeHead(200, {'Content-Type': 'text/plain charset=utf-8'})// 解析url參數(shù)var params = url.parse(req.url,true).queryres.write('網(wǎng)站名: '+params.name)res.write('\n')res.write('網(wǎng)站url: '+ params.url)res.end() }).listen(8000)瀏覽器中訪問http://localhost:8000/?name=百度&url=www.baidu.com
得到響應(yīng)
POST請(qǐng)求
node沒有設(shè)置專門對(duì)post的請(qǐng)求,一直等待用戶輸入開銷比較大,而是采用了監(jiān)聽用戶向服務(wù)器發(fā)送數(shù)據(jù)寫入的方式實(shí)現(xiàn)
var http = require('http') var fs = require('fs') var querystring = require('querystring')var postHTML fs.readFile('index.html',function (err,data) {if (err){console.error(err)}else{postHTML = data} })http.createServer(function (req, res) {var data = ""req.on('data', function (chunk) {data += chunk})// 數(shù)據(jù)讀完之后解析req.on('end',function () {// 解析參數(shù)console.log(data)data = querystring.parse(data)// 寫響應(yīng)頭res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'})if (data['name'] && data['url']){res.write("網(wǎng)站名"+ data['name'])res.write("<br>")res.write("網(wǎng)站url"+ data['url'])}else{res.write(postHTML)}res.end()}) }).listen(8000)index.html
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Sample Page</title> </head> <body> <form method="post">網(wǎng)站名:<input name="name"><br>網(wǎng)站url:<input name="url"><br><input type="submit"> </form> </body> </html>訪問localhost:8000后輸入表單內(nèi)容并提交,服務(wù)器會(huì)給出相應(yīng)
轉(zhuǎn)載于:https://www.cnblogs.com/fanghao/p/7818367.html
總結(jié)
以上是生活随笔為你收集整理的NodeJS Web模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 推荐一个很好用的脚本session sn
- 下一篇: spring 之 lookup-met