一、端口號的含義
1、端口號的作用
端口號是用於標識主機中特定進程的數字,分配給客戶端或服務端的進程。對於HTTP而言,端口號默認為80。而我們所熟知的127.0.0.1:8080,則是本機IP地址(回送地址),加上其它服務程序所佔用的端口號8080。
2、示例代碼:
const http = require('http');
const hostname = '127.0.0.1';
const port = 8080;
http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
二、回送地址的使用
1、回送地址的作用
回送地址,即127.0.0.1,其作用是將網絡數據包返回給發包者的地址。可以通過回送地址實現在本機進行服務端程序測試,以及在一個沒有獨立IP地址的計算機上測試客戶端程序。
2、示例代碼:
const http = require('http');
const hostname = '127.0.0.1';
const port = 8080;
http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World');
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
三、HTTP協議的特點
1、HTTP協議的無狀態性
HTTP協議是無狀態的,即兩次請求之間沒有關聯。這樣一來,服務端無法知道現在請求與上次請求是否來自同一個客戶端。可以通過Cookie、Session等機制實現服務端記錄客戶端信息。
2、HTTP協議的請求方式
HTTP協議的請求方式有GET、POST、PUT、DELETE等。GET方式用於獲取資源,POST方式用於提交資源,PUT方式用於更新資源,DELETE方式用於刪除資源。
3、示例代碼:
const http = require('http');
const hostname = '127.0.0.1';
const port = 8080;
http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
if(req.method === 'GET') {
res.end('This is a GET request!');
} else if(req.method === 'POST') {
res.end('This is a POST request!');
} else {
res.end('This is not a GET or POST request!');
}
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
四、路由的概念與應用
1、路由的含義
路由是指為了實現不同URL地址請求的分發而設立的一種機制。在Node.js中,使用路由實現對不同請求的不同響應。這樣一來,就可以實現實現更加靈活的請求頁面響應。
2、示例代碼:
const http = require('http');
const url = require('url');
const hostname = '127.0.0.1';
const port = 8080;
const route = {
'/': function(req, res) {
res.end('This is the home page');
},
'/about': function(req, res) {
res.end('This is the about page');
},
'/contact': function(req, res) {
res.end('This is the contact page');
}
};
http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
let pathname = url.parse(req.url).pathname;
if(route[pathname]){
route[pathname](req, res);
} else {
res.end('404 Page Not Found');
}
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
五、模板引擎的使用
1、模板引擎的作用
模板引擎是指以標籤或代碼為基礎,結合數據渲染生成具有結構、樣式和功能的HTML頁面的工具。在Node.js中,常用的模板引擎有handlebars和ejs。
2、示例代碼(使用ejs):
const http = require('http');
const ejs = require('ejs');
const hostname = '127.0.0.1';
const port = 8080;
const tpl = `
Content:
`;
http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
const data = {
title: 'EJS Demo',
heading: 'Hello EJS',
content: 'This is an example of using EJS!'
};
const html = ejs.render(tpl, data);
res.end(html);
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/159320.html
微信掃一掃
支付寶掃一掃