源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

nodejs基础应用

  • 时间:2022-11-26 00:59 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:nodejs基础应用
[b]一、第一个nodejs应用[/b] [b]n1_hello.js[/b] [code]console.log('hello word!');[/code] 在命令行cmd中执行该文件(在该文件处打开命令行): [b]node n1_hello.js[/b] 在命令行cmd返回结果: [b]hello word![/b] [b]二、nodejs基本格式[/b]
//步骤一:引入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/');
[b]三、nodejs调用函数[/b] [b]-----------------调用本地函数-----------------------------[/b]
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');
}
[b]-----------------调用外部函数-----------------------------[/b] [b]注意:外部函数必须写在module.exports中,exports 是模块公开的接口[/b] [b]------------(1)仅调用一个函数-----------[/b] [b]主程序中:[/b]
var http = require('http');
var otherfun = require("./models/otherfuns.js");//调用外部页面的fun2
http.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/');
[b]otherfuns.js中[/b]
function fun2(res){
 console.log('fun2');
 res.write('你好!,我是fun2');
}
module.exports = fun2;//只支持一个函数
[b]------------(2)调用多个函数-----------[/b] [b]主程序中:[/b]
var http = require('http');
var otherfun = require("./models/otherfuns.js");//调用写函数的外部页面otherfuns.js
http.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/');
}
[b]otherfuns.js中[/b]
module.exports={
 fun2:function(res){//匿名函数
  console.log('fun2');
  res.write('你好!,我是fun2');//在页面中输出
 },
 fun3:function(res){
  console.log('fun3');
  res.write('你好!,我是fun3');
 }, 
   ......
}
 
[b]四、nodejs路由初步[/b] [b]主程序n4_rout.js:[/b]
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/');
[b]在命令行cmd中执行该文件,在访问:[/b][url=http://localhost:8000/][b]http://localhost:8000/[/b][/url][b],在此输入路由地址,如下图,并观察命令行。[/b] [img]http://files.jb51.net/file_images/article/201702/201723105816517.png?20171311551[/img] [b]五、nodejs读取文件[/b] [b]主程序:[/b]
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/');
[b]optfile.js中:[/b]
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)同步读取文件时:    [img]http://files.jb51.net/file_images/article/201702/201723110050918.png?2017131153[/img] (2)异步读取文件时:(常用)    [img]http://files.jb51.net/file_images/article/201702/201723110106585.png?20171311423[/img]    网页中:均为: [img]http://files.jb51.net/file_images/article/201702/201723110128424.png?20171311254[/img] 以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持编程素材网!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部