凌雪
2018-10-10
来源 :网络
阅读 1518
评论 0
摘要:本文将带你了解微信小程序开发之 Node.js (基础九) 函数 & (基础十) 路由,希望本文对大家学微信有所帮助。
本文将带你了解微信小程序开发之 Node.js (基础九) 函数 & (基础十) 路由,希望本文对大家学微信有所帮助。
微信小程序 Node.js (基础九) 函数function say(word){
console.log(word)
}
function execute(someFunction,value){
someFunction(value)
}
execute(say,"HelloWorld")1234567812345678// 匿名函数function execute(someFunction,value){
someFunction(value)
}
execute(function(word){console.log(word)},"HelloWorld")12341234var http = require("http");
function onRequest(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);123456789123456789 微信小程序 Node.js (基础十) 路由路由:根据请求的URL和其他需要的GET及POST参数来执行相应的代码。 我们需要的所有数据都会包含在request对象中。 为了解析这些数据,我们需要url和querystring模块。 url.parse(string).query
|
url.parse(string).pathname |
| |
| |
------ -------------------
//localhost:8888/start?foo=bar&hello=world
--- -----
| |
| |
querystring.parse(queryString)["foo"] |
|
querystring.parse(queryString)["hello"]1234567891011121312345678910111213// 服务器// server.js
var http = require("http")
var url = require("url")
function start(route) {
function onRequest(request,response){
var pathname = url.parse(request.url).pathname;
console.log("pathname:"+ pathname)
route(pathname)
response.writeHead(200,{"content-Type":"text/plain"})
response.write("helloWorld")
response.end();
}
http.createServer(onRequest).listen(8888)
console.log("程序结束")
}
// module.exports = start;
exports.start = start
12345678910111213141516171819202122231234567891011121314151617181920212223// 路由// router.js
function route (pathname){
console.log("About to route a request for:"+pathname)
}
exports.route = route// main.js
var server = require('./hello')
var router = require('./router')
server.start(router.route)12341234
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之微信频道!
喜欢 | 0
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号