微信小程序 Node.js (基礎(chǔ)九) 函數(shù)
function say(word){
console.log(word)
}
function execute(someFunction,value){
someFunction(value)
}
execute(say,"HelloWorld")
// 匿名函數(shù)
function execute(someFunction,value){
someFunction(value)
}
execute(function(word){console.log(word)},"HelloWorld")
var 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);
微信小程序 Node.js (基礎(chǔ)十) 路由路由:根據(jù)請(qǐng)求的URL和其他需要的GET及POST參數(shù)來執(zhí)行相應(yīng)的代碼。 我們需要的所有數(shù)據(jù)都會(huì)包含在request對(duì)象中。 為了解析這些數(shù)據(jù),我們需要url和querystring模塊。
url.parse(string).query
|
url.parse(string).pathname |
| |
| |
------ -------------------
http://localhost:8888/start?foo=bar&hello=world
--- -----
| |
| |
querystring.parse(queryString)["foo"] |
|
querystring.parse(queryString)["hello"]
// 服務(wù)器
// 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("程序結(jié)束")
}
// module.exports = start;
exports.start = start
// 路由
// 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)
|