node.js是服务器程序,写的js语句都将运行在服务器上。返回给客户的都是你已经处理好的纯HTML文本。
例如:
//req表示引包,引包就是引用自己的一个特殊功能
var http = require('http');
//创建服务器,参数是一个回调函数,表示如果有请求进来,要做什么
var server = http.createServer(function (req,res) {
//req表示请求,reqest;res表示响应,responsee
//设置HTTP头部,状态码为200,文件类型是html,字符集是utf-8
res.writeHead(200,{'Content-type':'text/html;charset=UTF-8'});
// alert('haha');
res.end('我想知道'+(1+2+3)+'是多少?');
// res.end('这是我的第一个node界面;');
});
//运行服务器,监听3000端口(端口号可以任该)
console.log('哈哈');
server.listen(8000,"127.0.0.1");
console.log(this);
console.log(exports);
上面的这个语句时运行在服务器端,当返回给客户端的时候是它的最终结果是它最终的的运行结果。
如果我们想修改程序,我们必须中断挂起的服务器,重新node 。
nodejs没有根目录的概念,它没有任何web容器。
var http = require('http');
var fs = require('fs');
//创建服务器,参数是一个回调函数,表示如果有请求进来则执行这个读文件操作
var server = http.createServer(function (request,respone) {
if(request.url == '/fang'){
fs.readFile('./test.html',function (err,data) {
respone.writeHead(200,{'Content-type':'text/html;charset=UTF-8'});
respone.end(data);
});
}else if(request.url == '/yuan'){
fs.readFile('./round.html',function (err,data) {
respone.writeHead(200,{'Content-type':'text/html;charset=UTF-8'});
respone.end(data);
})
}else if(request.url == '/aaaa.css'){
fs.readFile('./1.css',function (err,data) {
respone.writeHead(200,{'Content-type':'text/html;charset=UTF-8'});
respone.end(data);
})
} else if(request.url == '/01.jpg') {
console.log('haha');
fs.readFile('./01.jpg', function (err, data) {
respone.writeHead(200, {'Content-type': 'text/html;charset=UTF-8'});
respone.end(data);
})
} else{
console.log('haha');
respone.writeHead(404,{'Content-type':'text/html;charset=UTF-8'});
respone.end('对不起,没有这个页面。');
}
});
// console.log("haha");
server.listen(3000,'127.0.0.1');
在if()条件中的语句,你可能会认为存在fang,yuan等等文件夹,但是事实是这个是同目录下对应test.html,round.html的物理文件夹
URL和真实的物理文件夹是没有关系的。URL是通过了Node的顶层路由设计。
Node.js模块
为了使Node.js的文件可以相互调用,Node.js提供了一个简单的模块系统。模块是Node.js应用程序的基本组成部分,文件和模块是一一对应的。换句话来说,一个Node,js文件就是一个模块,这个文件可能是JS代码,也可能是JSON或者编译过的C/C++扩展。
创建模块
在Node.js中创建一个模块很简单,例如,我们来创建意味名为main.js的文件:
var hello = require('./hello');
hello.world();
以上实例中,代码require(‘./hello’)引入了当前目录下的hello.js文件
Node.js提供了exports和require两个对象,其中exports是模块的公共接口,require用于从而外部获取一个模块的接口。即所获取模块的exports对象。
exports.world = function () {
console.log('hello world');
}
在hello.js文件中通过exports对象,把world作为模块的访问接口,在main.js中通过require(‘./hello’)加载这个模块,然后就可以直接访问hello.js中exports对象的成员函数。
var hello = require('./hello.js');
hello.world();
在上面的例子中,hello.js 通过 exports 对象把 world 作为模块的访问接口,在 main.js 中通过 require(‘./hello’) 加载这个模块,然后就可以直接访 问 hello.js 中 exports 对象的成员函数了。
有时候我们只是想把一个对象封装到模块中,格式如下:
module.exports = function(){
//...
}
//hello.js
function Hello() {
var name;
this.setName = function (thyname) {
name = thyname;
};
this.sayHello = function () {
console.log('Hello'+name);
};
};
module.exports = Hello;
这样我们就可以在main.js中直接获取这个对象了。
//main.js
//这里的变量Hello是用于接收hello.js文件向外部提供的接口函数
var Hello = require('./hello');
hello = new Hello();
hello.setName('Marry');
hello.sayHello();
模块接口的唯一变化是使用module.exports = Hello代替了exports.world = function(){}。在外部引用该模块时,其接口对象就是要输出了Hello对象本身,而不是原先的exports。
Http模块
我们在代码中经常会使用这样的语句:
var http = require('http');
...
http.createServer(...);
Node.js中自带了一个叫做http的模块,我们在代码中请求它并且将它的返回值赋给一个本地变量。我们的本地变量就变成了一个拥有所有http模块所提供的公共方法的对象。
513

被折叠的 条评论
为什么被折叠?



