Node.js - Tena Tauira Tauira HTTP Ao Ao

I tenei tauira ka whakaatuhia me pehea te hanga i tetahi kaituku HTTP ma te whakamahi i te Node.js. Ka whakarongo te kaituku i te tauranga 1337, ka tukuna Hello, World! ki te tirotiro i runga i te tono GET.

Kia mahara, kaua ki te whakamahi i te tauranga 1337, ka taea e koe te whakamahi i tetahi nama tauranga i whiriwhiria e koe kaore i te whakamahia e tetahi atu ratonga.

Ko te kohinga http he Node.js kōwae matua (he waahanga kei roto i te maatauranga a Node.js, kaore e hiahiatia te whakauru rauemi taapiri).

Ko te http whakarato te kōwae i te taumahinga ki te hanga i tetahi kaitoha HTTP ma te whakamahi i te http.createServer() tikanga.

Hei waihanga i te tono, hangahia he konae me te waehere JavaScript e whai ake nei.

const http = require('http'); // Loads the http module http.createServer((request, response) => {

// 1. Tell the browser everything is OK (Status code 200), and the data is in plain text
response.writeHead(200, {
'Content-Type': 'text/plain'
});
// 2. Write the announced text to the body of the page
response.write('Hello, World! ');
// 3. Tell the server that all of the response headers and body have been sent
response.end(); }).listen(1337); // 4. Tells the server what port to be on

Penapenahia te konae me tetahi ingoa ingoa o te konae. I tenei keehi, mena ka tapaina e taatau hello.js ka taea e taatau te whakahaere i te tono ma te haere ki te raarangi ingoa kei roto te konae me te whakamahi i nga whakahau e whai ake nei:



node hello.js

Ka taea te whakauru i te kaituku i hangaia me te URL http://localhost:1337 ko http://127.0.0.1:1337 i roto i te pūtirotiro.

Ka puta mai he whaarangi paetukutuku maamaa me te Hello, World! tuhinga kei runga rawa, e whakaaturia ana i te Whakaahuamata i raro:

Tauira Tūmau Node.js