nodejs

作者: overisover | 来源:发表于2016-12-29 15:14 被阅读0次

nodejs组成
1 ,基本语法
2,file
3,http
4,buffer

可以访问系统

require('');导入另一个node.js

一个文件的变量,只能在同一个文件操作

当前模块属性

调用方式比较像全局变量
其实不是

//__filename: 当前文件解析过后的绝对路径
console.log(__filename); //不同文件中,__filenam的值可能不一样

## 模块加载系统

### require('模块')

文件: 1.js
```js
console.log('module1')

相同路径下文件: 2.js

require('./1.js')
$node 2.js

可以看到打印了'module1',证明加载了1.js

module对象

保存提供和当前模块有关的一些信息

console.log(module)

module.exports

moudle对象下的字对象
我们可以通过这个对象把一个模块的局部变量
进行提供访问

文件1.js

var a = 1;
module.exports.aa = a; //输出a  ,才能在另一个文件访问a
//也可以 module.exports.aa=5; 直接定义a的值

文件2.js

var m1 = require('./1'); //保存访问的结果
console.log(m1.aa); //1 //打印a

module.exports是一个对象
所以我也可以这么写

module.exports= {
    aa: a
}

也可以是一个函数

module.exports = function (){

}

内置模块

相关:
request 请求
response 响应
fs 访问文件
http:
请求函数参数
Content-Type: text/html;charset=utf-8
常见的媒体格式类型如下:
text/html : HTML格式
text/plain :纯文本格式
text/xml : XML格式
image/gif :gif图片格式
image/jpeg :jpg图片格式
image/png:png图片格式

// //内置文件模块
// var fs=require('fs');
// fs.readFile('test.js',function(err,data){
//  console.log(data.toString())//读取 text.txt 文件里面的内容
// })
//内置http模块
var http=require('http');
var server=http.createServer(function(request,response){
    response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});//中文输入
    response.end('ni好')
})//新建服务器
server.listen(8088,'0.0.0.0');//监听端口
console.log('your Server running on 0.0.0.0: 8088')
//console.log('Server running on port 8088')//开启端口

启用服务打开HTML页面


var fs=require('fs');
var http=require('http');
var server=http.createServer(function(req,res){
    fs.readFile('./about.html',function(err,data){
        res.writeHead(200,{
            'Content-Type':'text/html;charser=utf-8'})
        res.end(data);
    })
});
server.listen(8089,'0.0.0.0');
console.log('your server is runing on 0.0.0.0:8089');

url的组成


//url的组成
<schema>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<hash>
https://www.baidu.com/s?wd=sdf&inputT=1408&rsv_sug4=1409#about.html



<schema>
https

<host>
www.baidu.com

<port>
80

<path>
s

<query>
wd=sdf&
inputT=1408&
rsv_sug4=1409

<hash>

相关文章

  • nodejs 到底是什么?

    理解 NodeJs Nodejs 自己使用了Openssl.在Nodejs 0.6之前, Nodejs是动态链接到...

  • centos7.3安装vue-cli

    1、安装vue需要安装nodejs,先去nodejs官网下载nodejs,https://nodejs.org/e...

  • NodeJS-简介&配置

    NodeJS-简介&配置 NodeJS NodeJS 中文网 NodeJS API 一、客户端的JavaScrip...

  • gulp最佳入门@小四

    一、安装nodeJs 说明:gulp是基于nodeJS,理所当然需要安装nodeJS; 安装:打开nodejs官网...

  • 项目构建---全步骤

    nodeJS安装 1.使用bower必须要安装nodeJS,因为bower就是用nodeJS编写的,nodeJS是...

  • 笔记 第六天 nodejs模块

    nodejs模块 nodejs 的文件操作 nodejs的io键盘交互 nodejs的url判断渲染模板 node...

  • nodeJs的下载及安装

    一、下载nodeJs 1. nodeJs的下载路径 (1)nodeJs官网:http://nodejs.cn/(2...

  • nodejs笔记

    nodejs教程 :http://www.runoob.com/nodejs/nodejs-tutorial.ht...

  • Cordova入门配置

    [TOC] Cordova 安装 安装NodeJS下载NodeJS https://nodejs.org/en/...

  • NodeJS 学习资料

    nodejs资源汇总(新手)从零开始nodejs系列文章Nodejs提炼与升华(一) 前言、Nodejs简介Nod...

网友评论

      本文标题:nodejs

      本文链接:https://www.haomeiwen.com/subject/ykrjvttx.html