NodeJS是什么?
简单的说 Node.js 就是运行在服务端的 JavaScript。
Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台。
Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常快,性能非常好。
events 模块只提供了一个对象: events.EventEmitter。EventEmitter 的核心就是事件触发与事件监听器功能的封装。
测试下安装环境:
dos下命令行:npm
查看npm的版本:npm -v
查看node.js版本命令行:node -v
node.js是一个基于Chrome v8引擎的JavaScript运行环境,Node.js使用了一个事件驱动,非阻塞式i/o的模型,使其轻量又高效。
node.js的包管理器npm,是全球最大的开源系统
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){ //清除第2此访问
console.log('访问');
response.write('hello,world');
response.end('hell,世界');//不写则没有http协议尾,但写了会产生两次访问
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
var http = require('http');
var otherfun = require('./models/otherfuns.js');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){ //清除第2此访问
otherfun.controller(request,response);
otherfun.call(response);
response.end('');
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
//---普通函数
function fun1(res){
res.write("你好,我是fun1");
}
function controller(req,res){
//res.write("发送");
call('hello',req,res);
res.end("");
}
function call(res){
console.log('call');
}
module.exports = controller; //只支持一个函数
/*
//支持多个函数
module.exports={
getVisit:function(){
return visitnum++;
},
add:function(a,b){
return a+b;
}
}
*/
node.js调用模块
function User(){
this.id;
this.name;
this.age;
this.enter=function(){
console.log("进入图书馆");
}
}
模块的调用
var http = require('http');
// var User=require('./models/User');
var Teacher = require("./models/Teacher');
http.createServer(function (request,response) {
response.writeHead(200,{'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){//清除第2此访问
teacher = new Teacher(1,'小红',30);
teacher.teacher(response);
response.end("");
}
}).listen(8000);
function User(id,name,age) {
this.id=id;
this.name=name;
this.enter=function(){
console.log("haha");
}
}
module.exports = User;
// models/Teacher.js
var User = require('./User');
function Teacher(id,name,age) {
User.apply(this,[id,name,age]);
this.teach=function(res){
res.write(this.name+"dashu");
}
}
module.exports = Teacher;
node.js路由
var http = require('http');
var url = require('url');
var router = require('/router');
http.createServer(function(request,response){
respnse.writeHead(200, {'Content-Type': 'text/html'; charset=utf-8"});
if(request.url!=="/favicon.ico"){
var pathname=url.parse(request.url).pathname;
// console.log(pathname);
pathname = pathname.replace(/\//, ");// 替换掉前面的
// console.log(pathname);
router[pathname](request,response);
response.end(");
}
}).listen(8000);
module.exports={
login: function(req,res){
res.write("我是login");
}
zhuce: function(req,res){
res.write("我是注册");
}
}
读取文件:
var http = require('http');
var optfile=require('./models/optfile');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
if(request.url!=="/favicon.ico"){
console.log('访问');
response.write("hello");
optfile.readfile();
response.end('hello");
}
}).listen(8000);
var fs = require('fs');
module.exports = {
readfile:funciton(path){
fs.readFile(path, function(err,data){
if(err){
console.log(err);
}else{
console.log(data.toString());
}
});
console.log("dashu");
},
readfileSync:function(path){
var data=fs.readFileSync(path,'utf-8');
console.log("同步方法");
return data;
}
}