Node.js 开发入门。

Node.js 是把js用到后台开发,比较方便流行。今天学习了,有些心得和知识整理如下。

一.用js开发,所以先介绍一下入门的上手快的js知识

1.js数据类型,js是弱类型语言,不需要显式定义int,string什么的,而且可以随时改变类型。如:

name="hello, world!";

name=10;

name=false;

js可以随时根据语境更换语言类型,可用typeof 查看类型。


2.node.js 输出函数:console.log(name),该函数返回值,undefined,undefined 跟null不同,null是没有,是一个值的类型,要定义,undefined指的是没有定义没有声明。


3.流程控制,跟c++,java 没有什么不同,故略过。提一下,===与==有些不同,js中===只允许同样的类型同样的结果才会返回true,针对false,null,“”,undefined这样的东西就可以很好的排除。

4.数组定义:

(1)

var website=[];

website[0]=1;
website[1]="hello";
console.log(website);

(2)直接法:var website2=[1,2,"hello"]

(3)new +push(push 相当于cpp或java的append)

var website3=new Array();
website[0]=2;

website3.push("22");

(4)有向后插入就有向前插入,unshift().

(5)删除。

删除最后一个:pop()

删除第一个元素:shift()

删除中间一些元素,splice(2,1)第一个是起点,第二个参数是以这个起点的几个元素。

5。function! js中的function是一个对象,正因为如此,以至于出神入化!

(1)跟java,c++一样的定义方法不过要加function:

function add(p1,p2){
  var website=[];
website[0]=p1;
website[1]=p2;
return website;
}


(2)神奇的匿名:变量就是函数!变量就是函数名!函数的本质就是对象或者变量

var remove=function(p1){
console.log(p1);
return p1;

}  

调用:var return_value=remove("hello");

注意最后没有;

(3)函数既然是对象,就可以传给另一个函数!

function output(p1,p2,p3,p4,p5){

return [p1,p2,p3,p4,p5]

}


function read(callback,p1,p2,p3,p4,p5){
console.log("read")
callback(p1,p2,p3,p4,p5)
}

read(output,aaa,bob,ccc,ddd,eee)

把函数和参数一起传过去。

(4)函数既然是个对象,就可以在函数内部再定义函数和调用函数。


6.面向对象:{}表示对象。

(1)定义1:

var website={} 空对象

website.domaniMain="hellow "//添加属性
website.name="jieson";
website.email="fdf";
website.age=3;
website.click=2000;

//添加方法属性:get set方法
website.setDomainName=function(domainName){
this.domainName=domainName
}
website.getDomainName=function(){
return this.domainName;
}

//调用。

website.setDomainName("hello")
console.log(website.getDomainName())

(2)定义2:直接写

var website1={

domainName:"dfdfdf",

name:"dfdff",

age:3,

setDomainName:function(dn){this.domainName=dn;},

getDomainName:function(){return this.domainName}

};

(3).定义3:构造函数

function WebSite(domainName,name,email,age,click){
this.domainName=domainName;//对象属性赋值
this.name=name;

 ...
this.setDomainName=function(df){this.domainName=df}

this.getDomainName=function(){return this.domainName}
...

}
var website3=new WebSite(aaa,ddd,,,fff,ff);
console.log(website3)

(4)将一些方法没有定义在对象中,而是注册方法。

function JSWebSite(domainName,name,email,age,click){

this.domName=domainName;
this.name=name;

}


JSWebSite.prototype.setDomanName=function(domainName){this.domainName=domainName}
JSwebSite.prototype.getDomainName=function(){return this.domainName}


var website4 =new JSWebSite(dfdf,dfdf,dfdfdf)


将方法没有定义在对象里,而是注册方法
website4.setDomanName(dfdf)
website4.getDomanName(d)

二。

1.好了有了以上准备工作,可以讲第一个node.js啦,helloworld程序

var http=require("http")//导入对象http

function requestListenser(req,res){
//一个请求,一个响应。
//当用户发生请求会被调用
console.log(req.url)
//打印用户的url

res.write("hello world!")//浏览器上写数据
res.end();//关闭连接

}


var server=http.createSever(requestListenser);

server.listen(7798);

//完成服务器创建。

在浏览器中http://localhost:7798


若http://localhost:7798 则控制台输出/
若http://localhost:7798/df 则控制台输出/df

2.用node.js写一个静态响应网站。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值