无法在Cloud9中使用Node.js获得Hello World

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

我不知道如何使服务器响应Hello World。我什至都不知道IP地址是什么。 IP是否在我的终端的选项卡上列出?

我刚刚使用默认的Node.js模板创建了一个EC2环境。我是否需要预先设置更多内容?

https://i.stack.imgur.com/4m85x.png

node.js cloud9
1个回答
0
投票

尝试下面的解决方案,如果您需要任何解释,请告诉我:

const http = require("http");
const port = 3000; // make sure the port number is not used

const requestHandler = (req, res) => {   
    req.on('Error occurerd', (err) => {
        console.error(err);
        res.end();
    });
    res.end("Hello from AWS Cloud9!");
}

const server = http.createServer(requestHandler);

server.listen(port, () => {
    console.log(`Server is listening on port ${port}!`);
    // Run your script then copy past this url in your browser 127.0.0.1:3000
});
© www.soinside.com 2019 - 2024. All rights reserved.