Notification texts go here Contact Us Buy Now!

Cara Membuat HTTP Web Server Menggunkan Node.js

 


Tentu kamu tidak asing lagi dengan yang namanya webserver, jika kamu pernah menggunakan PHP ataupun HTLM untuk membuat website dan kemudian kamu mejalankannya dengan menggunakan localhost. Metode tersebut menggunkan web server.

Sering kali kita menjalankan webserver php ataupun HTML menggunkan aplikasi webserver seperti xampp, wampp, maupun Apache atau llS. Contoh kita membuat sebuah website dengan nama https://localhost/index.html.

Dengan menggunkan node kamu bisa membuat dan menjalankan webserver kamu sendiri, untuk membuat webserve di node tidak terlalu sulit. Pada pembahasan ini admin akan mencoba membuat weberver menggunkan nodejs, berikut adalah script yang bisa kamu gunakan untuk menjalankan webserver di nodejs. Silahkan buat file helloworld.js dan copy code di bawah ini kedalam file script.

const http = require('http')
const port = 3000

const server = http.createServer((request, response) => {
  response.writeHead(200, { 'Content-Type': 'text/plain' })
  response.end('Hello world!')
})

server.listen(port, () => {
  console.log(`server started on port ${port}`)
  console.log('press Ctrl-C to terminate....')
})

Untuk penjelasan script di atas, bisa kamu baca di bawah ini.

  • Untuk line 1 kita memanggil http bawaan nodejs dan di masukan kedalam variabel http.
  • Untuk port yang di gunakna yaitu port 3000.
Lalu jalankan filenya dengan menggunakan perintah.
$ node helloworld.js
Tunggu hingga prosesnya selesai, jika sudah silahkan buka http://localhost:3000/ ke dalam browser kamu.

Getting Info...

About the Author

IT Enthusiast

Posting Komentar

Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.