瀏覽器和服務器通訊須要http協議完成node
//1 引入http模塊
var http = require('http');
//2 建立服務器 請求 響應
var server = http.createServer(function(request,response){
console.log('有訪問');
response.write('abc');
response.end();
})
//3 監聽8080端口
server.listen(8080);
//4 終端輸入 node server.js啓動服務器
//5 瀏覽器測試 localhost:8080 頁面雖然無顯示,可是控制檯中每當有訪問會打印 有訪問
localhost:8080/1.html
/1.html
/favicon.ico 瀏覽器自動給你請求的網站小圖標
fs.readFile('aaa.txt',function(err,data){
console.log(data);
//<Buffer 73 66 61 73 6b 6c 66 6a 0a 31 31 31 31 0a 32 32 32 32 0a 33 33 33 33 0a>
console.log(data.toString());//把二進制轉爲 字符串
})
fs.writeFile('bbb.txt','new data',function(err,data){
console.log(err,'err');
console.log(data,'data');
})
req.url => '/index.html'
讀取 './www/index.html'
'./www'+req.url
var fileName = './www'+req.url;
<div style="background:red;">
div
</div>
const http = require('http');
const fs = require('fs');
var server = http.createServer(function(req,res){
var filename = './www' + req.url;
fs.readFile(filename,(err,data)=>{
if(err){
res.write('404');
}else{
res.write(data);//機器對機器原本就是二進制,因此不用toString
}
res.end();
})
})
server.listen(8080);
接收前臺的數據請求mysql
請求頭 header
請求體 content POST數據
const queryString = require('queryString');
var json = queryString.parse("username=yuonly&password=123123");
console.log(json);
const urlLib = require('url');
//沒有第二個參數時
var obj = urlLib.parse('http://www.zyx58.com/index?a=12&b=5');
console.log(obj);
//obj 結果
Url {
protocol: 'http:', 協議
slashes: true,
auth: null,
host: 'www.zyx58.com', 主機
port: null, 端口
hostname: 'www.zyx58.com', 主機名
hash: null,
search: '?a=12&b=5',
query: 'a=12&b=5',數據
pathname: '/index',地址
path: '/index?a=12&b=5',請求路徑
href: 'http://www.zyx58.com/index?a=12&b=5'
}
//第二個參數爲true時
var obj = urlLib.parse('http://www.zyx58.com/index?a=12&b=5',true);
console.log(obj);
Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.zyx58.com',
port: null,
hostname: 'www.zyx58.com',
hash: null,
search: '?a=12&b=5',
query: { a: '12', b: '5' }, 數據 query已經被解析爲json
pathname: '/index', //地址
path: '/index?a=12&b=5',
href: 'http://www.zyx58.com/index?a=12&b=5'
}
const http = require('http');
const urlLib = require('url');
http.createServer((req,res)=>{
var obj = urlLib.parse(req.url,true);
var url = obj.pathname;
var GET = obj.query;
console.log(url,GET);
}).listen(8080);
var http = require('http');
var queryString = require('queryString');
http.createServer((req,res)=>{
//post - req post 數據大,因此要分段發送
//data 每當有一段數據到達的時候,就會觸發一次
//end 數據徹底到達的時候觸發一次
var str = '';//等待接收數據
var i = 0;//驗證是否分不少次到達
req.on('data',function(data){
console.log(`第${i++}次到達`);
str += data;
})
req.on('end',function(){
// console.log(str);
var POST = queryString.parse(str);
console.log(POST);
})
}).listen(8080);
const http = require('http');
const fs = require('fs');
const queryString = require('queryString');
const urlLib = require('url');
http.createServer((req,res)=>{
//GET
var obj = urlLib.parse(req.url,true);
var url = obj.pathname;
const GET = obj.query;
//POST
var str = '';
var POST = {};
req.on('data',function(data){
str += data;
})
req.on('end',function(){
POST = queryString.parse(str);
console.log(url,GET,POST);
})
// url 要那個文件
// GET get數據
// POST post數據
// ================ 文件請求======
var filename = './www'+url;
fs.readFile(filename,function(err,data){
if(err){
res.write('404');
}else{
res.write(data);
}
res.end();
})
// localhost:8080/aaa.html
}).listen(8080);
文檔 : http://nodejs.cn/api/git
require: 引入模塊ajax
既能夠引入系統模塊,又能夠引入文件模塊,因此即使是當前目錄也要加上 ./
沒有全局變量,至關於
(function(require,exports,module){
var a = 12;
exports.a = 12;
})()
exports: 導出、輸出。一個一個輸出sql
module: 模塊。批量輸出東西npm
對外輸出一堆東西
exports.a = 12;
exports.b = 5;
exports.c = 99;
module.exports = {a:12,b:5,c:99}
console.log(module.exports === exports);//true:說明是一個東西
require('./mod1.js') .js能夠省略不寫json
自動下載依賴api
安裝
npm i mysql --save
卸載
npm uninstall mysql
require 1. 若是有 './' 從當前目錄找瀏覽器
- 若是沒有 ‘./’。首先從系統模塊中找,沒有,再去node_modules目錄中找
npm login
輸入用戶名、密碼、郵箱
npm whoami
npm publish
npm update test0528
npm unpublish --force