1. 程式人生 > >Node.js-建立Web伺服器和TCP伺服器

Node.js-建立Web伺服器和TCP伺服器

使用http模組建立Web伺服器

   Web伺服器的功能:

  • 接受HTTP請求(GET、POST、DELETE、PUT、PATCH)
  • 處理HTTP請求(自己處理,或請求別的程式處理)
  • 做出響應(返回頁面、檔案、各類資料等)

常見的Web伺服器架構:

  1. Nginx、Apache:負責接受HTTP請求,確定誰來處理請求,並返回請求的結果
  2. php-fpm / php模組:處理分配給自己的請求,並將處理結果返回給分配者  

常見請求種類:

  1. 請求檔案:包括靜態檔案(網頁、圖片、前端JavaScript檔案、css檔案...),及由程式處理得到的檔案
  2. 完成特定的操作:如登入、獲取特定資料等          

Node.js的Web伺服器:

  1. 不依賴其他特定的Web伺服器軟體(如Apache、Nginx、IIS......)
  2. Node.js程式碼處理請求的邏輯
  3. Node.js程式碼負責Web伺服器的各種“配置”         

使用Express建立Web伺服器

  • 簡單的Express伺服器
  • 靜態檔案服務
  • 路由
  • 中介軟體
        簡單的Express伺服器:
	var express = require('express');

        var app = express();

        app.get('', function(req, res){
		res.end('hello\n');
	});

	app.listen(18001, function afterListen(){
	 	console.log('express running on http://localhost:18001');
	});	
靜態檔案範圍:          網頁、純文字、圖片、前端JavaScript程式碼、CSS樣式表文件、媒體檔案、字型檔案 使用Express訪問靜態檔案     
app.use(express.static('./public'));
路由:
  1. 將不同的請求,分配給相應的處理函式
  2. 區分:路徑、請求方法
三種路由實現方法:
  • path:比較簡單
  • Router:比較適合同一個路由下的多個子路由
  • route:比較適合API
var express = require('express');
var morgan = require('morgan');

var app = express();

app.use(express.static('./public'));
app.use(morgan());

app.get('', function(req, res){
	res.end('hello\n');
});

// Ronter路由
var Router = express.Router();

// http://example.com/post/add
// http://example.com/post/list
Router.get('/add', function(req, res){
	res.end('Router /add\n');
});
Router.get('/list', function(req, res){
	res.end('Router /list\n');
});

app.use('/post', Router);

// route路由
app.route('/article')
	.get(function(req, res){
		res.end('route /article get\n');
	})
	.post(function(req, res){
		res.end('route /article post\n')
	});

//http://example.com/news/123

app.param('newsId', function(req, res, next, newsId){
	req.newsId = newsId;
	next();
});

app.get('/news/:newsId', function(req, res){
	res.end('newsId: ' + req.newsId+ '\n');
});
	

app.listen(18001, function afterListen(){
	console.log('express running on http://localhost:18001');
});
中介軟體
  • Connect:Node.js的中介軟體框架
  • 分層處理
  • 每層實現一個功能

建立TCP伺服器

  • 使用net模組建立TCP伺服器
  • 使用telnet連線TCP伺服器
  • 使用net建立TCP客戶端
tcp.js:
var net = require('net');

const PORT = 18001;
const HOST = '127.0.0.1'

var clientHandler = function(socket){
	console.log('someone connected');

	socket.on('data', function dataHandler(data){
		console.log(socket.remoteAddress, socket.remotePort, 'send', data.toString());
		socket.write('server received\n');
	});

	socket.on('close', function(){
		console.log(socket.remoteAddress, socket.remotePort, 'disconnected');
	})
};

var app = net.createServer(clientHandler);

app.listen(PORT, HOST);
console.log('TCP server running on tcp://', HOST, ':', PORT);
tcpClient.js:
var net = require('net');

const HOST = '127.0.0.1';
const PORT = 18001;

var tcpClient = net.Socket();

tcpClient.connect(PORT, HOST, function(){
	console.log('connect success');
	tcpClient.write('this is tcp client by Node.js');
});

tcpClient.on('data', function(data){
	console.log('received: ', data.toString());
})