1. 程式人生 > >搭建一個最簡單的node服務器

搭建一個最簡單的node服務器

node string str console 參數 地址 param color json

搭建一個最簡單的node服務器

  1、創建一個Http服務並監聽8888端口

  2、使用url模塊 獲取請求的路由和請求參數

var http = require(‘http‘);
var url = require(‘url‘);

http.createServer(function (req, res) {

    var pathname = url.parse(req.url).pathname; //解析路由請求地址
    var params = url.parse(req.url, true).query; //解析請求參數
    
    console.log(req.method
+" Request for " + pathname + " received."); //打印請求 res.writeHead(200, { ‘Content-Type‘: ‘text/plain‘ }); res.write(req.method); res.write(‘\npathname:‘+pathname); res.write(‘\nparams:‘+JSON.stringify(params)); res.end(); }).listen(8888); console.log(‘Server running at http://127.0.0.1:8888/‘);

搭建一個最簡單的node服務器