console.log(TAG, `server is listening on http ${httpPort} port.`);
});
-const headers = {
- // Set CORS
- 'Access-Control-Allow-Origin': '*',
- 'Access-Control-Allow-Methods': 'OPTIONS, POST, GET',
- 'Content-Type': 'application/json'
- /* add other headers as per requirement */
-};
-
-app.post('/api/js-offloading', function (request, response) {
- console.log(TAG, `Post msg : /api/js-offloading`);
-
- let body = [];
- request
- .on('data', chunk => {
- body.push(chunk);
- })
- .on('end', () => {
- body = Buffer.concat(body).toString();
- const jsonBody = JSON.parse(body);
-
- const offloadSocket = sockets.get(jsonBody.workerSocket);
- console.log(' offload socket ' + offloadSocket);
- if (offloadSocket !== undefined) {
- console.log(TAG, `start offloading : `);
- offloadSocket.emit(
- 'message',
- {
- message: {
- data: body,
- type: 'COMPUTE'
- }
- },
- function (data) {
- console.log(TAG, `result obtained from offload worker`);
- const jsonRet = data;
- if (jsonRet.errorCode === 'ERR_SCRIPT_EXECUTION_TIMEOUT') {
- response.writeHead(408, headers);
- response.end(JSON.stringify(jsonRet));
- } else if (jsonRet.errorCoode === 'UNKNOWN ERROR') {
- response.writeHead(400, headers);
- response.end(JSON.stringify(jsonRet));
- } else {
- response.writeHead(200, headers);
- response.end(JSON.stringify(jsonRet));
- console.log(TAG, jsonRet);
- }
- console.log(TAG, `responded to client`);
- }
- );
- } else {
- console.log(TAG, `no registered compute worker`);
- response.writeHead(404, headers);
- response.end();
- }
- });
-});
-
function getMyAddress() {
const interfaces = os.networkInterfaces();
const addresses = {};