一、第一個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)異步讀取文件時:(常用)

網頁中:均為:

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!
新聞熱點
疑難解答