node的function函数和路由代码的小例子
2024-09-30 04:17:04
1、node事件循环
事件:
const events=require("events");
emt=new events.EventEmitter(); function eventHandler(){
console.log("111");
console.log("222")
}
emt.on("eventName",eventHandler);
emt.emit("eventName");
2、模块自定义
function show(){
this.name='user1';
this.say=function(){
console.log("my name is "+this.name);
} }
module.exports=new show();
3、function函数,分为常用函数和匿名函数
1)、常用函数,如
Function show(){
Console.log(“123”);
}
2)、匿名函数,如
Show=function(){
Console.log(“123”);
}
Show();
4、node路由
代码示例如下:
const http=require("http");
const url=require("url"); cs=function(req,res){
ur=req.url;
if(ur!=="/favicon.ico"){
//arr=url.parse(ur);
//路由
path=url.parse(ur).pathname;
switch(path){
case "/user/add":
res.write("<h1>use add</h1>");
break;
case "/user/delete":
res.write("<h1>use delete</h1>");
break;
case "/user/select":
res.write("<h1>use select</h1>");
break;
default:
res.write("<h1>hello~</h1>")
}
}
//console.log(arr);
res.write("hello world");
res.end(); } http.createServer(cs).listen(888);
console.log("server is ok");
最新文章
- Shell特殊变量
- 试解析Tomcat运行原理(一)--- socket通讯
- php的socket通信
- bootstrap框架禁用谷歌字体
- 算法系列:geometry
- iOS系统架构
- [ZZ] Deferred Rendering and HDR
- Mongod(5):启动命令mongod参数说明
- Spring REST实践之Documenting REST Services
- Python 资源
- Html页面加回滚
- Linux内核学习笔记-1.简介和入门
- struts2 ValueStack(值栈)解析
- Memcached内存存储
- 利用flask 实现简单模版站
- 【BZOJ4003】【JLOI2015】城池攻占
- Git问题总结
- python day10 函数(第二篇)
- Shell脚本 | 截取包名
- ---转载---phython资料
热门文章
- 12.扩展:向量空间模型算法(Vector Space Model)
- python常用sys模块
- gRPC-Protocol语法指南
- vue下history模式刷新后404错误解决
- 设计模式PDF下载了4.0万本!那,再肝一本《Java面经手册》吧!
- uBuntu安装其他版本Python
- Matlab2016b安装流程
- P5911 [POI2004]PRZ (状态压缩dp+枚举子集)
- 【题解】[SDOI2016]征途
- 优质分享 | Spring Boot 入门到放弃!!!