Node.js - Hello World HTTP Server Muenzaniso

Mumuenzaniso uyu isu tinoratidza maitiro ekugadzira sevha yeHTTP tichishandisa Node.js. Seva ichateerera pachiteshi 1337, uye ichatumira Hello, World! kubhurawuza pane chikumbiro cheGET.

Ziva kuti, pachinzvimbo chekushandisa chiteshi 1337, unogona kushandisa chero nhamba yechiteshi yesarudzo yako iyo parizvino isiri kushandiswa nechero basa.

Iyo http module ndeye Node.js musimboti module (module inosanganisirwa muNode.js sosi, izvo hazvidi kuisa zvimwe zviwanikwa).

I http module inopa mashandiro ekugadzira sevha yeHTTP uchishandisa http.createServer() nzira.

Kugadzira iyo application, gadzira iyo faira ine inotevera JavaScript kodhi.

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

Sevha iyo faira ine chero zita refaira. Mune ino kesi, kana isu tikazvitumidza hello.js tinogona kumhanyisa iyo application nekuenda kudhairekitori iyo faira iri uye nekushandisa unotevera kuraira:



node hello.js

Iyo yakagadzirwa server inogona kuzowanikwa neiyo URL http://localhost:1337 kana http://127.0.0.1:1337 mubrowser.

Rakareruka peji rewebhu richaonekwa ne Hello, World! zvinyorwa kumusoro, sezvakaratidzwa mumufananidzo uri pasi apa:

Node.js Server Muenzaniso