在 Linux 上进行 JavaScript 跨平台开发
一、开发路线与适用场景
二、快速上手示例
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
node server.js,浏览器访问 http://127.0.0.1:3000。mkdir my-electron-app && cd $_
npm init -y
npm i --save-dev electron
在 package.json 中添加脚本:"start": "electron main.js"。const { app, BrowserWindow } = require('electron');
function createWindow() {
const win = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: false, contextIsolation: true } });
win.loadFile('index.html');
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit(); });
app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) createWindow(); });
<!doctype html>
<html><body><h1>Hello Electron</h1></body></html>
npm start。npx create-expo-app my-rn-app,进入项目后 npx expo start,可用 iOS/Android 模拟器或真机运行。三、跨平台适配要点
process.platform 返回 ‘win32’/‘linux’/‘darwin’(Windows 为 win32 即使 64 位);os.type()/os.release()/os.version()/os.arch() 获取更细粒度信息(如 x64/arm64)。const os = require('os');
console.log(process.platform, os.arch()); // e.g. linux x64
四、工具链与发布建议