nodejs-函数&路由
2023-09-11 14:14:01 时间
函数------------------------------------------------------------
基本函数
function say(word) {
console.log(word);
}
function execute(someFunction, value) {
someFunction(value);
}
execute(say, "Hello");
匿名函数function execute(someFunction, value) {
someFunction(value);
}
execute(function(word){ console.log(word) }, "Hello");
路由-------------------------------------------------------------
url.parse(string).query
|
url.parse(string).pathname |
| |
| |
------ -------------------
http://localhost:8888/start?foo=bar&hello=world
--- -----
| |
| |
querystring.parse(queryString)["foo"] |
|
querystring.parse(queryString)["hello"]
router.js 文件代码:
function route(pathname) {
console.log("About to route a request for " + pathname);
}
exports.route = route;
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("Request for " + pathname + " received.");
route(pathname);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;
index.js 文件代码
var server = require("./server");
var router = require("./router");
server.start(router.route);
相关文章
- nodeJS - 定义全局变量
- 阿里云centos配置nginx和nodejs
- nodejs npm常用命令
- EasyRMS录播管理服务器项目实战:windows上开机自启动NodeJS服务
- electron/nodejs实现调用golang函数
- Pre and post exit in ABAP method and Nodejs
- 如何启用nodejs request模块的调试模式
- nodejs项目npm start背后的工作原理
- 推荐一个一分钟就可以搭建好的静态文件服务器,基于nodejs
- NodeJs——(16)用Nodejs 4.X版本,制作一个微博网站(多图,详细步骤)
- NodeJs——(15)动态视图助手
- 【nodejs原理&源码赏析(6)】深度剖析cluster模块源码与node.js多进程
- nodejs 守护 forever