nodejs基础应用
一、第一个nodejs应用
n1_hello.js
console.log('hello word!');
在命令行煤矿管理局中执行该文件(在该文件处打开命令行):
节点n1_hello.js
在命令行煤矿管理局返回结果:
你好!
二、nodejs基本格式
//步骤一:引入需要模块,要求指令载入超文本传送协议(超文本传输协议的缩写)模块var http=require(' http ');//步骤二:创建服务器http.createServer(函数(请求,响应){ //发送超文本传送协议头部//HTTP状态值: 200 : OK //内容类型:文本/html响应。writeHead(200,{“内容类型”:)文本/html;chase t=utf-8;'});//步骤三:接受请求与响应请求if(request.url!=='/fav图标。ico '){ 0.//发送响应数据回应。end(" ");//必须有,没有则没有协议尾}}).听(8000);//终端打印如下信息console.log('运行于http://127.0.0.1:8000/'的服务器);三、nodejs调用函数
-调用本地函数-
var http=require(' http ');http.createServer(函数(请求,响应){ response.writeHead(200,{ ' Content-Type ' : ' text/html;chase t=utf-8;'});if(request.url!=='/favicon.ico'){ fun1(响应);//发送响应数据回应。end(" ");}}).听(8000);//终端打印如下信息console.log('运行于http://127.0.0.1:8000/'的服务器);fun1函数(RES){控制台。日志(' fun 1 ');res.write('你好,我是乐趣1’;} - 调用外部函数-
注意:外部函数必须写在模块。导出中出口是模块公开的接口
- (1)仅调用一个函数-
主程序中:
var http=require(' http ');var otherfun=require(' ./模型/其他功能。js ';//调用外部页面的fun2http.createServer(函数(请求,响应){ response.writeHead(200,{ ' Content-Type ' : ' text/html;chase t=utf-8;'});if(request.url!=='/favicon.ico'){ otherfun(响应);//支持一个函数时回应。end(" ");}}).听(8000);//终端打印如下信息console.log('运行于http://127.0.0.1:8000/'的服务器);otherfuns.js中
fun2函数(RES){控制台。日志(' fun 2 ');res.write('你好!我是乐趣2’;} module.exports=fun2//只支持一个函数- (2)调用多个函数-
主程序中:
var http=require(' http ');var otherfun=require(' ./模型/其他功能。js ';//调用写函数的外部页面otherfuns.jshttp.createServer(函数(请求,响应){ response.writeHead(200,{ ' Content-Type ' : ' text/html;chase t=utf-8;'});if(request.url!=='/favicon.ico'){ //todo以对象。方法名调用otherfun.fun2(回应);otherfun.fun3(回应);//todo以字符串调用对应函数(结果同上)//otherfun['fun2'](响应);//otherfun['fun3'](响应);回应。end(" ");}}).听(8000);//终端打印如下信息console.log('运行于http://127.0.0.1:8000/'的服务器);}otherfuns.js中
模块。导出={ fun 2:函数(RES){//匿名函数控制台。日志(' fun 2 ');res.write('你好!我是乐趣2’;//在页面中输出},好玩的3:功能(RES){控制台。日志(' fun 3 ');res.write('你好!我是乐趣3’;},}四、nodejs路由初步
主程序n4_rout.js:
var http=require(' http ');//引入全球资源定位器(统一资源定位符)模块var URL=必选(' URL ');http.createServer(函数(请求,响应){ response.writeHead(200,{ ' Content-Type ' : ' text/html;chase t=utf-8;'});if(request.url!=='/fav图标。ico’){ var pathname=URL。解析(请求。网址).路径名;pathname=pathname.replace(/\//,' ');//替换掉前面的/console.log(路径名);回应。end(" ");}}).听(8000);//终端打印如下信息console.log('运行于http://127.0.0.1:8000/'的服务器);在命令行煤矿管理局中执行该文件,在访问:http://localhost:8000/,在此输入路由地址,如下图,并观察命令行。
五、nodejs读取文件
主程序:
var http=require(' http ');var optfile=require('。/models/opt file’);//导入文件HTTP.createserver(函数(请求,响应){//发送HTTP头//HTTP状态值: 200 : OK //内容类型: text/html response . write head(200,{ ' content-type ' : ' text/html;chase t=utf-8;'});if(request.url!=='/favicon.ico'){//清除第二次访问optfile.readfilesync('。/view/log in . html ');//调用readfileSync()方法同步读取文件//optfile.readfile('。/view/log in . html ',响应);//调用readfile readfile()方法response.end('ok!');//todo不写console.log不带协议尾('主程序完成!' );}}).听(8000);//终端打印以下信息console.log('运行于http://127.0.0.1:8000/'的服务器);optfile.js中的:
var fs=require(' fs ');//Node import文件系统模块(fs)语法导入类模块. exports={ readfilesync : function(路径){//read vardata=fs.readfilesync(路径,' utf-8 ')同步;//用中文读取同步文件路径路径console.log同步方法完成。’);},readfile:函数(路径){//read fs.readfile(路径,函数(err,数据){if (err) {console。错误(err);}else{ console.log('异步读取: ' data . tostring());} });Console.log('异步方法已完成执行。);},}结果:命令行cmd
(1)同步读取文件时:
(2)异步读取文件时:(常用)
网页中的:都是:
以上就是本文的全部内容。希望本文的内容能给大家的学习或工作带来一些帮助,也希望多多支持我们!
版权声明:nodejs基础应用是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。