官术网_书友最值得收藏!

  • Effective DevOps with AWS
  • Yogesh Raheja Giuseppe Borgese Nathaniel Felsen
  • 228字
  • 2021-07-23 16:27:30

Running a Node.js Hello World application

Now that the node is installed, we can create a simple Hello World application. Here is the code for creating this:

var http = require("http") http.createServer(function (request, response) {
// Send the HTTP header
// HTTP Status: 200 : OK
// Content Type: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'})
// Send the response body as "Hello World" response.end('Hello World\n')
}).listen(3000)

// Console will print the message console.log('Server running')

Feel free to copy this into a file. Alternatively, if you want to save time, download this from GitHub:

[ec2-user@ip-172-31-22-52 ~]$ 
wget https://raw.githubusercontent.com/yogeshraheja/Effective-DevOps-with-AWS/master/Chapter02/helloworld.js -O /home/ec2-user/helloworld.js
--2018-08-19 13:06:42-- https://raw.githubusercontent.com/yogeshraheja/Effective-DevOps-with-AWS/master/Chapter02/helloworld.js
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.200.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.200.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 384 [text/plain]
Saving to: ‘/home/ec2-user/helloworld.js’

/home/ec2-user/helloworld.js 100%[=====================================================================================>] 384 --.-KB/s in 0s

2018-08-19 13:06:42 (37.9 MB/s) - ‘/home/ec2-user/helloworld.js’ saved [384/384]


[ec2-user@ip-172-31-22-52 ~]$

In order to run the Hello World application, we are now simply going to run the following code:

[ec2-user@ip-172-31-22-52 ~]$ node helloworld.js  
Server running 

If everything goes well, you will now be able to open this in your browser at the following link: http://your-public-dns-name:3000. Or in my case, this will be found here: http://ec2-34-201-101-26.compute-1.amazonaws.com:3000. You will then be able to see the result, as follows:

We will now stop the execution of the Hello World web application with Ctrl + C in your Terminal window.

主站蜘蛛池模板: 偃师市| 呼伦贝尔市| 淄博市| 普安县| 鄯善县| 社旗县| 莒南县| 岚皋县| 甘孜| 德保县| 称多县| 仲巴县| 寿宁县| 阳东县| 荣昌县| 大田县| 九寨沟县| 盐山县| 恩平市| 台东县| 遂昌县| 叙永县| 什邡市| 清新县| 镇平县| 岳普湖县| 锦州市| 新建县| 九江县| 塔城市| 抚顺市| 寿阳县| 闸北区| 阿鲁科尔沁旗| 临沧市| 建水县| 平塘县| 湘潭市| 且末县| 厦门市| 泾源县|