1 , node.js开启服务
let http = require('http') ;
http.createServer(function(req,res){
if(req.url !== '/favicon.ico'){
<!--设置访问头-->
res.writeHead(200,{'Content-Type':'text/html ; charset=utf-8'}) ;
<!--注意charset=utf-8此处不能有空格-->
res.write('hello') ;
res.end('world') ;
<!--有head必须要加res.end()-->
}
}).listen(8080) ;
2 , 函数的调用
(1):调用本页面的函数
http.createServer(function(req,res){
res.writeHead(200,{'Content-Type':'text/html;charset=utf-8'}) ;
<!--调用函数-->
fn1(res) ;
res.end() ;
})
<!--函数-->
function fn1(res){
res.write('hello') ;
}
(2):调用另外页面的函数
1):导出函数
module.exports = {
fn1(res){
res.write('我是南安') ;
},
fn2(res){
res.write('我是南安1') ;
}
}
2):函数的调用
let ontherFn = ('./pablic/fn1') ;
ontherFn['fn1'](res) ;
<!--ontherFn.fn1(res)-->
ontherFn['fn2'](res) ;
3 , 调用模块
<!--teacher模块-->
let User = require('./user') ;
function teacher(name,age,loving) {
this.loving = loving ;
User.call(this,name,age) ;
this.getLoving = function (res) {
res.write('我的姓名是:' + name + '我的年纪是:'+ age + '我的最爱是:' + loving) ;
}
}
module.exports = teacher ;
<!--调用teacher.js模块-->
let User = require('./pablic/teacher') ;
user = new User('北月',24,'game') ;
4 , 关于url模块的引用
(1):引入
let url = require('url') ;
(2):url.parse(url)
<!--返回值是url的json对象-->
<!--
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: null,
query: null,
pathname: '/logon',
path: '/logon',
href: '/logon'
-->
5 , 路由的使用
(1):配置router.js
<!--
module.exports = {
logon:function (res) {
res.write('login~~~~~') ;
},
zhuce:function (res) {
res.write('注册') ;
}
} ;
-->
(2):引入路由
let LuYou = require('./pablic/luyou.js') ;
LuYou[pathName](response) ;
node.js随笔(1)
最新推荐文章于 2022-11-07 21:57:06 发布