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开发微信公众号后台服务实例
Sep 03 NodeJs
Nodejs中调用系统命令、Shell脚本和Python脚本的方法和实例
Jan 01 NodeJs
nodejs中向HTTP响应传送进程的输出
Mar 19 NodeJs
nodejs实现邮件发送服务实例分享
Mar 29 NodeJs
NodeJS 实现手机短信验证模块阿里大于功能
Jun 19 NodeJs
NodeJS链接MySql数据库的操作方法
Jun 27 NodeJs
详解HTTPS 的原理和 NodeJS 的实现
Jul 04 NodeJs
nodejs微信扫码支付功能实现
Feb 17 NodeJs
Nodejs模块的调用操作实例分析
Dec 25 NodeJs
Nodejs + Websocket 指定发送及群聊的实现
Jan 09 NodeJs
nodejs各种姿势断点调试的方法
Jun 18 NodeJs
如何利用nodejs实现命令行游戏
Nov 24 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
与空气斗智斗勇的经典《Overlord》,传说中的“无稽之谈”
2020/04/09 日漫
一首老MP3,致敬WAR3经典
2021/03/08 魔兽争霸
让PHP支持断点续传的源码
2010/05/16 PHP
Windows下的PHP 5.3.x安装 Zend Guard Loader教程
2014/09/06 PHP
zen_cart实现支付前生成订单的方法
2016/05/06 PHP
PHP随机生成中文段落示例【测试网站内容时使用】
2020/04/26 PHP
Javascript 实用小技巧
2010/04/07 Javascript
基于jquery的时间段实现代码
2012/08/02 Javascript
url参数中有+、空格、=、%、&、#等特殊符号的问题解决
2013/05/15 Javascript
js中settimeout方法加参数
2014/02/28 Javascript
分享一个插件实现水珠自动下落效果
2016/06/01 Javascript
JavaScript实现翻页功能(附效果图)
2017/02/16 Javascript
Bootstrap table使用方法总结
2017/05/10 Javascript
jquery中有哪些api jQuery主要API
2017/11/20 jQuery
vue中v-for加载本地静态图片方法
2018/03/03 Javascript
Vue中用props给data赋初始值遇到的问题解决
2018/11/27 Javascript
nodejs同步调用获取mysql数据时遇到的大坑
2019/03/02 NodeJs
js实现点击上传图片并设为模糊背景
2020/08/02 Javascript
vue组件添加事件@click.native操作
2020/10/30 Javascript
vue图片裁剪插件vue-cropper使用方法详解
2020/12/16 Vue.js
精确查找PHP WEBSHELL木马的方法(1)
2011/04/12 Python
使用Python脚本对Linux服务器进行监控的教程
2015/04/02 Python
python处理图片之PIL模块简单使用方法
2015/05/11 Python
Python数据结构之图的应用示例
2018/05/11 Python
Python获取系统所有进程PID及进程名称的方法示例
2018/05/24 Python
Python爬虫抓取指定网页图片代码实例
2020/07/24 Python
Python Opencv图像处理基本操作代码详解
2020/08/31 Python
CSS3 @keyframes简单动画实现
2018/02/24 HTML / CSS
The Hut德国站点:时装、家居用品、美容等
2016/09/23 全球购物
Why do we need Unit test
2013/01/03 面试题
Java如何支持I18N?
2016/10/31 面试题
2014年化验员工作总结
2014/11/18 职场文书
先进班组事迹材料
2014/12/25 职场文书
导游词开场白
2015/01/31 职场文书
年会主持人开场白台词
2015/05/29 职场文书
学生会主席任命书
2015/09/21 职场文书