如何在共享服务器上部署nodejs应用

问题描述 投票:0回答:1

我已经在adionisjs框架上实现了应用程序,我想将其部署在godaddy共享服务器上,有什么办法吗?

node.js deployment
1个回答
0
投票

您不能直接在共享服务器上部署nodejs应用程序,因为共享服务器配置的服务器名称不带端口,因此,您可以使用PHP运行nodejs应用程序。

<?php
//Choose JS file to run
$file = 'node_modules/jt-js-sample/index.js';
//Spawn node server in the background and return its pid
$pid = exec('PORT=49999 node/bin/node ' . $file . ' >/dev/null 2>&1 & echo $!');
//Wait for node to start up
usleep(500000);
//Connect to node server using cURL
$curl = curl_init('http://127.0.0.1:49999/');
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//Get the full response
$resp = curl_exec($curl);
if($resp === false) {
    //If couldn't connect, try increasing usleep
    echo 'Error: ' . curl_error($curl);
} else {
    //Split response headers and body
    list($head, $body) = explode("\r\n\r\n", $resp, 2);
    $headarr = explode("\n", $head);
    //Print headers
    foreach($headarr as $headval) {
        header($headval);
    }
    //Print body
    echo $body;
}
//Close connection
curl_close($curl);
//Close node server
exec('kill ' . $pid);
© www.soinside.com 2019 - 2024. All rights reserved.