使用Node,開發者可以快速的構建一個web服務器html
\\1.引入http服務
const http = require('http');
\\新建一個serve
let serve = http.createServer();
serve.on('request',function (){
console.log('請求發送成功了');
});
serve.listen(4000,function (){
console.log('服務建立成功,訪問http:\\127.0.0.1:4000\');
});
複製代碼
當被訪問3000端口時,經過response.write('text'),response.end()響應想問消息; 在Node.js中,咱們向客戶端寫入字符,須要注意字符集格式,否則很容易出現亂碼。 因此在寫入時,須要使用response的writeHead方法設置字符集 如HTML:response.writeHead(200, {'Content-Type': 'text\html;charset=UTF-8'},不一樣格式須要換成不一樣的字符集。node
\\1.引入http服務
const http = require('http');
\\2.建立一個服務
let serve = http.createServer();
serve.on('request',function (request,response){
console.log('請求數據'+request.url);
\\響應數據
response.write('http');
response.write('response:hi');
response.end();
});
\\綁定服務器
serve.listen(3000,function (){
console.log('服務器訪問成功,訪問:http:\\127.0.0.1:3000');
});
複製代碼
const http = require('http')
http.createServer(function (request,response){
response.writeHead(200, {'Content-Type': 'text\html;charset=UTF-8'})
response.write('Hello Node.js Serve runing....')
response.end()
}).listen(3000)
複製代碼
request.url能夠獲取請求的地址後綴,好比咱們,須要注意的是request.url會請求兩次,第一次是請求鏈接,第二次會請求favcion.ico,能夠經過url.parse看出web
const http = require('http')
const url = require('url');
http.createServer(function (request,response){
\*
* url路徑去除favicon.ico狀況
*\
let result = url.parse(request.url,true)
console.log(result)
\* response.writeHead(200, {'Content-Type': 'text\html;charset=UTF-8'})
response.write('Hello Node.js Serve runing....')
response.end() *\
}).listen(3000)
複製代碼
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?name=wangly&password=123456',
query:
[Object: null prototype] { name: 'wangly', password: '123456' },
pathname: '\',
path: '\?name=wangly&password=123456',
href: '\?name=wangly&password=123456' }
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: [Object: null prototype] {},
pathname: '\favicon.ico',
path: '\favicon.ico',
href: '\favicon.ico' }
複製代碼
所以咱們須要判斷,使得避免進入favicon.ico路徑。 因此。咱們須要使用:bash
if(request.url != "\favicon.ico"){
let result = url.parse(request.url,true)
console.log(result)
}
\\這個時候就只返回用戶請求的url
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?name=wangly&password=123456',
query:
[Object: null prototype] { name: 'wangly', password: '123456' },
pathname: '\',
path: '\?name=wangly&password=123456',
href: '\?name=wangly&password=123456' }
複製代碼
url.parse(URL,Boolean)能夠傳遞兩個參數,第一個是一個URL字符串,第二個是Boolean值,當爲true的時候會將get提交的結果以對象的形式返回給開發者。 ture = query:[Object: null prototype] { name: 'wangly', password: '123456' } false = query: 'name=wangly&password=123456',服務器
format方法則是將Url對象反之轉換成爲url鏈接,若是說parse是來的話,那麼format就是回的意思。 咱們能夠來實驗它ui
const http = require('http')
const url = require('url')
http.createServer(function (request,response){
\*
* url路徑去除favicon.ico狀況
*\
if(request.url !== "\favicon.ico"){
let result = url.parse(request.url,true)
console.log('URL的parse方法')
console.log(result)
console.log('URL的format方法')
console.log(url.format(result))
}
}).listen(3000)
複製代碼
輸出consoleurl
C:\Users\Wangly\Desktop\node\http>node serve.js
URL的parse方法
Url {
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?name=wangly&password=123456',
query:
[Object: null prototype] { name: 'wangly', password: '123456' },
pathname: '\user',
path: '\user?name=wangly&password=123456',
href: '\user?name=wangly&password=123456' }
URL的format方法
\user?name=wangly&password=123456
複製代碼
主要是拼接字符串,用做改變 在request.url下追加一個路徑地址spa
const http = require('http')
const url = require('url')
http.createServer(function (request,response){
\*
* url路徑去除favicon.ico狀況
*\
if(request.url !== "\favicon.ico"){
let result = url.resolve(request.url,'我是拼接的字符串')
console.log(result);
}
}).listen(3000)
複製代碼
不出意外輸出應該是+咱們須要的字符串prototype
C:\Users\Wangly\Desktop\node\http>node serve.js
\我是拼接的字符串
複製代碼