Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated server to run on http and serve the dist.zip file dirrctly on dev mode #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions .vscode/getNet.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
const { networkInterfaces } = require('os');
const { networkInterfaces } = require('os')

module.exports = async (mode = 'dev') => {
const ip = getIp();
const port = '5500';
const src = `https://${ip || '10.0.0'}:${port}`;
console.log('Server starting at: ', src);
return { ip, port };
};
const ip = getIp()
const port = '5500'
const src = `http://${ip || '10.0.0'}:${port}`
console.log('Server starting at: ', src)
return { ip, port }
}

function getIp() {
const nets = networkInterfaces();
let ip = '';
const nets = networkInterfaces()
let ip = ''

Object.keys(nets).some((name) => {
return nets[name].find((net) => {
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4;
if (net.family === familyV4Value && !net.internal) {
ip = net.address;
return ip;
}
});
});
Object.keys(nets).some(name => {
return nets[name].find(net => {
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4
if (net.family === familyV4Value && !net.internal) {
ip = net.address
return ip
}
})
})

return ip;
return ip
}
104 changes: 53 additions & 51 deletions .vscode/start-server.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,69 @@
/* eslint-disable import/no-extraneous-dependencies */
const fs = require('fs');
const path = require('path');
const liveServer = require('live-server');
const getNet = require('./getNet');
const fs = require('fs')
const path = require('path')
const liveServer = require('live-server')
const getNet = require('./getNet')

const serverCrt = path.resolve(__dirname, 'server.crt');
const serverKey = path.resolve(__dirname, 'server.key');
const serverCrt = path.resolve(__dirname, 'server.crt')
const serverKey = path.resolve(__dirname, 'server.key')

main();
main()

async function main() {
const { ip: host, port } = await getNet('dev');
process.cwd = () => __dirname;
liveServer.start({
open: false,
port,
host,
cors: true,
root: '../',
ignore: 'node_modules,platforms,plugins',
file: 'index.html',
https: {
cert: fs.readFileSync(serverCrt),
key: fs.readFileSync(serverKey),
passphrase: '1234',
},
middleware: [(req, res, next) => {
const url = req.originalUrl;
const www = '../platforms/android/app/src/main/assets/www/';
const { ip: host, port } = await getNet('dev')
process.cwd = () => __dirname
liveServer.start({
open: false,
port,
host,
cors: true,
root: '../',
ignore: 'node_modules,platforms,plugins',
file: 'dist.zip',
https: {
cert: fs.readFileSync(serverCrt),
key: fs.readFileSync(serverKey),
passphrase: '1234'
},
middleware: [
(req, res, next) => {
const url = req.originalUrl
const www = '../platforms/android/app/src/main/assets/www/'

if (url === '/cordova.js') {
const file = path.resolve(__dirname, www, 'cordova.js');
sendFile(res, file);
return;
}
if (url === '/cordova.js') {
const file = path.resolve(__dirname, www, 'cordova.js')
sendFile(res, file)
return
}

if (url === '/cordova_plugins.js') {
const file = path.resolve(__dirname, www, 'cordova_plugins.js');
sendFile(res, file);
return;
}
if (url === '/cordova_plugins.js') {
const file = path.resolve(__dirname, www, 'cordova_plugins.js')
sendFile(res, file)
return
}

next();
}],
});
next()
}
]
})

process.send('OK');
process.send('OK')
}

function sendFile(res, filePath) {
if (fs.existsSync(filePath)) {
const stat = fs.statSync(filePath);
if (fs.existsSync(filePath)) {
const stat = fs.statSync(filePath)

res.writeHead(200, {
'Content-Type': 'application/javascript',
'Content-Length': stat.size,
});
res.writeHead(200, {
'Content-Type': 'application/javascript',
'Content-Length': stat.size
})

const readStream = fs.createReadStream(filePath);
readStream.pipe(res);
return;
}
const readStream = fs.createReadStream(filePath)
readStream.pipe(res)
return
}

res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end(`ERROR cannot get ${filePath}`);
res.writeHead(404, { 'Content-Type': 'text/plain' })
res.end(`ERROR cannot get ${filePath}`)
}