Skip to content

Instantly share code, notes, and snippets.

@AKosmachyov
Created January 21, 2023 17:33
Show Gist options
  • Save AKosmachyov/711237ef7d79f284f116a34f85395efa to your computer and use it in GitHub Desktop.
Save AKosmachyov/711237ef7d79f284f116a34f85395efa to your computer and use it in GitHub Desktop.
NodeJS simple static server

How to use

  1. create copy of the file index.js;
  2. run NodeJS server:
node index.js

AdHoc server

  1. save manifest.plist file in the same folder as index.js;
  2. create folder apps;
  3. save AppName.ipa and app icons(512.png, 57.png) to the apps folder;
  4. update URLs inside manifest.plist file: <string>http://<ip-address>:8125/apps/AppName.ipa</string>;
  5. run server: node index.js
  6. On iPad/iOS open url: itms-services://?action=download-manifest&url=<http://<ip-address>:8125/manifest.plist

iPad/iOS will download following files:

  • manifest.plist
  • AppName.ipa
  • 57.png
  • 512.png
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = process.env.PORT || 8125;
http.createServer(function (request, response) {
logRequest(request);
var filePath = '.' + request.url;
if (filePath == './') {
filePath = './index.html';
}
var extname = path.extname(filePath);
var contentType = 'text/html';
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.css':
contentType = 'text/css';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
contentType = 'image/jpg';
break;
case '.wav':
contentType = 'audio/wav';
break;
case '.ipa':
contentType = 'application/octet-stream';
break;
case '.plist':
contentType = 'text/xml';
break;
}
fs.readFile(filePath, function (error, content) {
if (error) {
if (error.code == 'ENOENT') {
fs.readFile('./404.html', function (error, content) {
response.writeHead(200, {
'Content-Type': contentType
});
response.end(content, 'utf-8');
});
} else {
console.log('error', error)
response.writeHead(500);
response.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n');
response.end();
}
} else {
response.writeHead(200, {
'Content-Type': contentType
});
response.end(content, 'utf-8');
}
});
}).listen(PORT);
console.log('Server running', `http://localhost:${PORT}/`);
function logRequest(request) {
const ip = request.headers['x-forwarded-for'] || request.socket.remoteAddress || null;
const path = request.url
const method = request.method;
const timeStamp = getTimeStamp();
console.log('log:', timeStamp, method, path, 'from ip', ip);
}
function getTimeStamp() {
var today = new Date();
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
return date + ' ' + time;
}
{
"name": "static-ios-adhoc",
"version": "0.0.1",
"description": "Server for .ipa files",
"main": "index.js",
"engines": {
"node": "16.x"
},
"scripts": {
"start": "node index.js"
},
"author": "AKosmachyov",
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment