# autojs-koa **Repository Path**: yashujs/autojs-koa ## Basic Information - **Project Name**: autojs-koa - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-03-29 - **Last Updated**: 2023-10-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 使用场景 手机上搭建服务器 ## 效果展示 效果 ## autojs版本 9.0.2发布日期: 20210329 ![autojs版本](./other/autojs版本.png) ## 原理 autojspro9.0.2内置了nodejs, nodejs又是服务器语言, 所以就可以在手机上搭建服务器了 我们使用最轻的koa来搭建服务器 ## 步骤 1. 看文档, koa官网 > https://www.koajs.com.cn/ 2. 在电脑上先跑通koa, 就几行代码, **main.js** ```js // autojs使用nodejs的标志, 必须在第一行 "nodejs"; const Koa = require("koa"); const app = new Koa(); app.use(async (ctx) => { ctx.body = "Hello World"; }); app.listen(3000); console.log("http://127.0.0.1:3000"); ``` 3. 然后**node main.js**, 然后浏览器访问**http://127.0.0.1:3000**, 就可以看到helloworld了 4. 电脑跑通以后, 我们把调试好的项目文件都保存到手机, 不要忘了 **package.json**, **node_modules** 5. 然后在手机上运行项目, 再打开手机自带浏览器访问**http://你的手机内网ip:3000**, 同样可以看到helloworld 6. autojs的node项目, 也支持打包, 打包后同样可以使用. ### 其他 获取内网ip代码 ```js importClass("java.net.InetAddress"); importClass("java.net.NetworkInterface"); importClass("java.net.Inet6Address"); let intranetIP = getIntranetIP(); setClip("http://" + intranetIP + ":3000"); alert("http://" + intranetIP + ":3000"); function getIntranetIP() { // 获取内网IP地址 let networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { let networkInterface = networkInterfaces.nextElement(); let inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { let inetAddress = inetAddresses.nextElement(); if (inetAddress instanceof Inet6Address) { continue; } let ip = inetAddress.getHostAddress(); log(ip); if (!"127.0.0.1".equals(ip)) { return inetAddress.getHostAddress(); } } } } ``` 手机自带默认浏览器打开指定网址 ```js let url='http://你的手机内网ip:3000' app.openUrl(url) ``` ## 总结 经过以上步骤, 我们的helloworld服务器就搭建完成 ## 微信公众号 AutoJsPro教程 ![officialaccount.jpg](./other/officialaccount.jpg) ## QQ群 747748653 ![给我个面子小图](./other/给我个面子小图.jpg)