簡體   English   中英

ip.address()在aws上不起作用。 如何以編程方式獲取AWS EC2的IP地址?

[英]ip.address() does not work on aws. How to get the IP address of AWS EC2 programmatically?

我用Node / Express后端開發了一個React應用程序。 它基於以下入門工具包。 它在我的本地計算機上運行良好,但是當我在AWS上部署它時,它以錯誤的IP地址開頭。

我的配置文件

    const ip = require('ip')
    server_host : ip.address(),
    server_port : process.env.PORT || 3000,

在本地計算機上運行npm start-運行正常,並且以

    app:bin:server Server is now running at 192.168.1.5:3000.

但是當我將其放在EC2(52.72.209.201)上並啟動時不起作用,我得到的日志為

    app:bin:server Server is now running at 172.31.31.141:3000.

如果我在配置文件中硬編碼實例IP地址,則可以正常工作

    server_host : '52.72.209.201',
    server_port : process.env.PORT || 3000,

現在它開始並且運行良好

    app:bin:server Server is now running at 52.72.209.201:3000.

如何刪除硬編碼的IP地址?

您應該能夠從以下位置獲取公用IP地址:

要么:

您可以在外殼程序腳本(如啟動腳本)中使用curl來獲取它,例如將其存儲在環境變量或文件系統上的某個文件中。 或者,您可以在Node中使用請求

有關更多信息,請參見:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM