nodejs基礎應用,nodejs基礎
一、第一個nodejs應用
n1_hello.js
console.log('hello word!');
在命令列cmd中執行該檔案(在該檔案處開啟命令列):
node n1_hello.js
在命令列cmd返回結果:
hello word!
二、nodejs基本格式
//步驟一:引入require模組,require指令載入http模組var http = require('http');//步驟二:建立伺服器http.createServer(function (request, response) { // 發送 HTTP 頭部 // HTTP 狀態值: 200 : OK // 內容類型: text/html response.writeHead(200, {'Content-Type': 'text/html;chaset=utf-8;'});//步驟三:接受請求與響應請求 if(request.url!=='/favicon.ico'){ ...... // 發送響應資料 response.end('');//必須有,沒有則沒有協議尾 }}).listen(8000);// 終端列印如下資訊console.log('Server running at http://127.0.0.1:8000/');
三、nodejs調用函數
-----------------調用本地函數-----------------------------
var http = require('http');http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/html;chaset=utf-8;'}); if(request.url!=='/favicon.ico'){ fun1(response); // 發送響應資料 response.end(''); }}).listen(8000);// 終端列印如下資訊console.log('Server running at http://127.0.0.1:8000/');function fun1(res){ console.log('fun1'); res.write('hello,我是fun1');}
-----------------調用外部函數-----------------------------
注意:外部函數必須寫在module.exports中,exports 是模組公開的介面
------------(1)僅調用一個函數-----------
主程式中:
var http = require('http');var otherfun = require("./models/otherfuns.js");//調用外部頁面的fun2http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/html;chaset=utf-8;'}); if(request.url!=='/favicon.ico'){ otherfun(response);//支援一個函數時 response.end(''); }}).listen(8000);// 終端列印如下資訊console.log('Server running at http://127.0.0.1:8000/');
otherfuns.js中
function fun2(res){ console.log('fun2'); res.write('你好!,我是fun2');}module.exports = fun2;//只支援一個函數
------------(2)調用多個函數-----------
主程式中:
var http = require('http');var otherfun = require("./models/otherfuns.js");//調用寫函數的外部頁面otherfuns.jshttp.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/html;chaset=utf-8;'}); if(request.url!=='/favicon.ico'){ //todo 以對象.方法名調用 otherfun.fun2(response); otherfun.fun3(response); //todo 以字串調用對應函數(結果同上) //otherfun['fun2'](response); //otherfun['fun3'](response); response.end(''); }}).listen(8000);// 終端列印如下資訊console.log('Server running at http://127.0.0.1:8000/');}
otherfuns.js中
module.exports={ fun2:function(res){//匿名函數 console.log('fun2'); res.write('你好!,我是fun2');//在頁面中輸出 }, fun3:function(res){ console.log('fun3'); res.write('你好!,我是fun3'); }, ......}
四、nodejs路由初步
主程式n4_rout.js:
var http = require('http');//引入url模組var url = require('url');http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/html;chaset=utf-8;'}); if(request.url!=='/favicon.ico'){ var pathname = url.parse(request.url).pathname; pathname=pathname.replace(/\//,'');//替換掉前面的/ console.log(pathname); response.end(''); }}).listen(8000);// 終端列印如下資訊console.log('Server running at http://127.0.0.1:8000/');
在命令列cmd中執行該檔案,在訪問:http://localhost:8000/,在此輸入路由地址,如,並觀察命令列。
五、nodejs讀取檔案
主程式:
var http = require('http');var optfile=require('./models/optfile');//匯入檔案http.createServer(function (request, response) { // 發送 HTTP 頭部 // HTTP 狀態值: 200 : OK // 內容類型: text/html response.writeHead(200, {'Content-Type': 'text/html;chaset=utf-8;'}); if(request.url!=='/favicon.ico'){//清除第2次訪問 optfile.readfileSync('./views/login.html');//同步調用讀取檔案readfileSync()方法 //optfile.readfile('./views/login.html',response);//非同步步調用讀取檔案readfile()方法 response.end('ok!!!!!');//todo 不寫沒有協議尾 console.log('主程式執行完畢!'); }}).listen(8000);// 終端列印如下資訊console.log('Server running at http://127.0.0.1:8000/');
optfile.js中:
var fs=require('fs');//Node 匯入檔案系統模組(fs)文法 匯入fs操作檔案的類module.exports={ readfileSync:function(path){ // 同步讀取 var data = fs.readFileSync(path,'utf-8');//以中文讀取同步檔案路徑path console.log("同步方法執行完畢。"); }, readfile:function(path){ // 非同步讀取 fs.readFile(path,function (err, data) { if (err) { console.error(err); }else{ console.log("非同步讀取: " + data.toString()); } }); console.log("非同步方法呼叫執行完畢。"); },}
結果:命令列cmd中
(1)同步讀取檔案時:
(2)非同步讀取檔案時:(常用)
網頁中:均為:
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的協助,同時也希望多多支援幫客之家!