2

在服务器配置之后,我有一个 IIS 服务器在我的 EC2 实例中侦听端口 80。我可以从我的弹性 IP 访问它。但我关闭了 IIS 服务器,希望在端口 80 上启动我的节点服务器。

现在,两者都不起作用!我的节点应用程序侦听端口 80,但无法通过弹性 IP 从外部访问。我试图在 IIS 管理器中启动 IIS 服务器,它看起来像打开了。但它无法在实例内(使用私有 IP)或从外部访问。

我能做些什么来解决它?

谢谢

服务器.js

/*******************************************************************************************/
/*CREATE AND START SERVER*/
/*******************************************************************************************/
var fs = require('fs');
var html = fs.readFileSync('./public/index.html');
var server=http.createServer(function(req,res){

    //res.write(html); // load the single view file (angular will handle the page changes on the front-end)
    //res.end();
    // application -------------------------------------------------------------
    app.get('*', function(req, res) {
        res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
    });

});

server.listen(80,'localhost');
4

2 回答 2

1

我建议使用相同的密钥对和安全组启动一个新实例。然后将弹性 IP 移动到新实例上。

启动 Node JS假设 AWS Linux AMI

sudo yum update
sudo yum upgrade
sudo yum install gcc
yum groupinstall "Development tools"
wget -c https://nodejs.org/dist/v5.2.0/node-v5.2.0.tar.gz
#This is to download the source code.
tar -xzf node-v$ver.tar.gz
cd node-v$ver
./configure && make && sudo make install

然后你的app.js

var port = process.env.PORT || 80,
    http = require('http'),
    fs = require('fs'),
    html = fs.readFileSync('index.html');
var index = require('./index');

var server = http.createServer(function (req, res) {
    console.log(req.method + "sdf " + req.url + res);
    if (req.method === 'POST') {
        var body = '';

        req.on('data', function(chunk) {
            body += chunk;
        });

        req.on('end', function () {
            res.writeHead(200, 'OK', {'Content-Type': 'text/plain'});
            res.write (html);
            res.end();
        });
    } else {
        res.writeHead(200);
        res.write(html);
        res.end();
    }
});

server.listen(port);
console.log('Server running at http://127.0.0.1:' + port + '/');

还有你的index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <p>Hello</p>
    </body>
</html>

这只是一个非常简单的 Node JS 实例

对于更强大的设置,我建议您前往 EBS 并启动他们的一个示例。这要容易得多。

希望这可以帮助

更新

问题:用户试图http在端口 80 上创建服务器侦听并运行 express 格式

解决方案:要么 http使用监听端口,要么express同时使用 - 两者都不允许程序运行

于 2016-01-12T23:17:07.643 回答
0

您仍然遇到与我在此处的评论中描述的问题相同的问题。

当你这样做时:

server.listen(80,'localhost')

您正在告诉服务器侦听来自localhost 的请求,这意味着您明确忽略了所有外部请求。只需删除该参数即可监听来自所有地址的请求:

server.listen(80)
于 2016-01-14T22:50:52.543 回答