nodejs基础应用


Posted in NodeJs onFebruary 03, 2017

一、第一个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");//调用外部页面的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/');

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.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/');
}

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基础应用

五、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)同步读取文件时:

   nodejs基础应用

(2)异步读取文件时:(常用)

   nodejs基础应用

   网页中:均为:

nodejs基础应用

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!

NodeJs 相关文章推荐
轻松创建nodejs服务器(8):非阻塞是如何实现的
Dec 18 NodeJs
NodeJS学习笔记之FS文件模块
Jan 13 NodeJs
nodejs中实现阻塞实例
Mar 24 NodeJs
nodejs实现遍历文件夹并统计文件大小
May 28 NodeJs
Highcharts+NodeJS搭建数据可视化平台示例
Jan 01 NodeJs
初探nodeJS
Jan 24 NodeJs
基于Nodejs利用socket.io实现多人聊天室
Feb 22 NodeJs
详解nodejs微信公众号开发——1.接入微信公众号
Apr 10 NodeJs
nodejs中art-template模板语法的引入及冲突解决方案
Nov 07 NodeJs
nodejs实现截取上传视频中一帧作为预览图片
Dec 10 NodeJs
nodejs一个简单的文件服务器的创建方法
Sep 13 NodeJs
在nodejs中创建child process的方法
Jan 26 NodeJs
nodejs基础知识
Feb 03 #NodeJs
windows 下安装nodejs 环境变量设置
Feb 02 #NodeJs
图片上传之FileAPI与NodeJs
Jan 24 #NodeJs
初探nodeJS
Jan 24 #NodeJs
进阶之初探nodeJS
Jan 24 #NodeJs
用nodejs搭建websocket服务器
Jan 23 #NodeJs
NodeJS遍历文件生产文件列表功能示例
Jan 22 #NodeJs
You might like
PHP函数in_array()使用详解
2014/08/20 PHP
PHP中抽象类、接口的区别与选择分析
2016/03/29 PHP
thinkPHP5.0框架命名空间详解
2017/03/18 PHP
Yii框架的redis命令使用方法简单示例
2019/10/15 PHP
电子商务网站上的常用的js放大镜效果
2011/12/08 Javascript
如何在指定的地方插入html内容和文本内容
2013/12/23 Javascript
JavaScript调用ajax获取文本文件内容实现代码
2014/03/28 Javascript
js计算任意值之间随机数的方法
2015/01/16 Javascript
JS制作简单的三级联动
2015/03/18 Javascript
Bootstrap实现提示框和弹出框效果
2017/01/11 Javascript
jQuery插件JWPlayer视频播放器用法实例分析
2017/01/11 Javascript
基于nodejs+express4.X实现文件下载的实例代码
2017/07/13 NodeJs
js使用ajax传值给后台,后台返回字符串处理方法
2018/08/08 Javascript
微信小程序页面缩放式侧滑效果的实现代码
2018/11/15 Javascript
Nuxt.js之自动路由原理的实现方法
2018/11/21 Javascript
基于AngularJS拖拽插件ngDraggable.js实现拖拽排序功能
2019/04/02 Javascript
微信小程序 下拉刷新及上拉加载原理解析
2019/11/06 Javascript
如何配置vue.config.js 处理static文件夹下的静态文件
2020/06/19 Javascript
angular共享依赖的解决方案分享
2020/10/15 Javascript
使用Python对SQLite数据库操作
2017/04/06 Python
Python3 处理JSON的实例详解
2017/10/29 Python
Python计算开方、立方、圆周率,精确到小数点后任意位的方法
2018/07/17 Python
基于python中theano库的线性回归
2018/08/31 Python
python的pip安装以及使用教程
2018/09/18 Python
python3实现在二叉树中找出和为某一值的所有路径(推荐)
2019/12/26 Python
Jupyter notebook 远程配置及SSL加密教程
2020/04/14 Python
欧缇丽美国官网:Caudalie美国
2016/12/31 全球购物
彼得罗夫美国官网:Peter Thomas Roth美国(青瓜面膜)
2017/11/05 全球购物
毕业生动漫设计求职信
2013/10/11 职场文书
技校个人求职信范文
2014/01/25 职场文书
认购协议书范本
2014/04/22 职场文书
优质服务口号
2014/06/11 职场文书
客户答谢会活动方案
2014/08/31 职场文书
手把手教你怎么用Python实现zip文件密码的破解
2021/05/27 Python
vue项目配置sass及引入外部scss文件
2022/04/14 Vue.js
Redis 限流器
2022/05/15 Redis