nodejs基礎(chǔ)應(yīng)用
一、第一個nodejs應(yīng)用
n1_hello.js
console.log('hello word!');
在命令行cmd中執(zhí)行該文件(在該文件處打開命令行):
node n1_hello.js
在命令行cmd返回結(jié)果:
hello word!
二、nodejs基本格式
//步驟一:引入require模塊,require指令載入http模塊 var http = require('http'); //步驟二:創(chuàng)建服務(wù)器 http.createServer(function (request, response) { // 發(fā)送 HTTP 頭部 // HTTP 狀態(tài)值: 200 : OK // 內(nèi)容類型: text/html response.writeHead(200, {'Content-Type': 'text/html;chaset=utf-8;'}); //步驟三:接受請求與響應(yīng)請求 if(request.url!=='/favicon.ico'){ ...... // 發(fā)送響應(yīng)數(shù)據(jù) response.end('');//必須有,沒有則沒有協(xié)議尾 } }).listen(8000); // 終端打印如下信息 console.log('Server running at http://127.0.0.1:8000/');
三、nodejs調(diào)用函數(shù)
-----------------調(diào)用本地函數(shù)-----------------------------
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); // 發(fā)送響應(yīng)數(shù)據(jù) 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'); }
-----------------調(diào)用外部函數(shù)-----------------------------
注意:外部函數(shù)必須寫在module.exports中,exports 是模塊公開的接口
------------(1)僅調(diào)用一個函數(shù)-----------
主程序中:
var http = require('http'); var otherfun = require("./models/otherfuns.js");//調(diào)用外部頁面的fun2 http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/html;chaset=utf-8;'}); if(request.url!=='/favicon.ico'){ otherfun(response);//支持一個函數(shù)時 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;//只支持一個函數(shù)
------------(2)調(diào)用多個函數(shù)-----------
主程序中:
var http = require('http'); var otherfun = require("./models/otherfuns.js");//調(diào)用寫函數(shù)的外部頁面otherfuns.js http.createServer(function (request, response) { response.writeHead(200, {'Content-Type': 'text/html;chaset=utf-8;'}); if(request.url!=='/favicon.ico'){ //todo 以對象.方法名調(diào)用 otherfun.fun2(response); otherfun.fun3(response); //todo 以字符串調(diào)用對應(yīng)函數(shù)(結(jié)果同上) //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){//匿名函數(shù) 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中執(zhí)行該文件,在訪問:http://localhost:8000/,在此輸入路由地址,如下圖,并觀察命令行。
五、nodejs讀取文件
主程序:
var http = require('http'); var optfile=require('./models/optfile');//導(dǎo)入文件 http.createServer(function (request, response) { // 發(fā)送 HTTP 頭部 // HTTP 狀態(tài)值: 200 : OK // 內(nèi)容類型: text/html response.writeHead(200, {'Content-Type': 'text/html;chaset=utf-8;'}); if(request.url!=='/favicon.ico'){//清除第2次訪問 optfile.readfileSync('./views/login.html');//同步調(diào)用讀取文件readfileSync()方法 //optfile.readfile('./views/login.html',response);//異步步調(diào)用讀取文件readfile()方法 response.end('ok!!!!!');//todo 不寫沒有協(xié)議尾 console.log('主程序執(zhí)行完畢!'); } }).listen(8000); // 終端打印如下信息 console.log('Server running at http://127.0.0.1:8000/');
optfile.js中:
var fs=require('fs');//Node 導(dǎo)入文件系統(tǒng)模塊(fs)語法 導(dǎo)入fs操作文件的類 module.exports={ readfileSync:function(path){ // 同步讀取 var data = fs.readFileSync(path,'utf-8');//以中文讀取同步文件路徑path console.log("同步方法執(zhí)行完畢。"); }, readfile:function(path){ // 異步讀取 fs.readFile(path,function (err, data) { if (err) { console.error(err); }else{ console.log("異步讀取: " + data.toString()); } }); console.log("異步方法執(zhí)行完畢。"); }, }
結(jié)果:命令行cmd中
(1)同步讀取文件時:
(2)異步讀取文件時:(常用)
網(wǎng)頁中:均為:
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
nodejs實現(xiàn)遍歷文件夾并統(tǒng)計文件大小
這篇文章主要介紹了nodejs實現(xiàn)遍歷文件夾并統(tǒng)計文件大小,下面使用nodejs的遍歷文件夾文件內(nèi)容,并且讀取所有的文件,并采取排序往大到小的順序進行輸出,需要的朋友可以參考下2015-05-05Nodejs探秘之深入理解單線程實現(xiàn)高并發(fā)原理
這篇文章主要介紹了Nodejs單線程實現(xiàn)高并發(fā)原理,對Node.js感興趣的同學(xué),可以參考下2021-04-04詳解如何使用koa實現(xiàn)socket.io官網(wǎng)的例子
這篇文章主要介紹了詳解如何使用koa實現(xiàn)socket.io官網(wǎng)的例子,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11Node使用koa2實現(xiàn)一個簡單JWT鑒權(quán)的方法
這篇文章主要介紹了Node使用koa2實現(xiàn)一個簡單JWT鑒權(quán)的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2021-01-01