Menjalankan Server Web NodeJs Di Termux
Kali ini kita akan mencoba menginstall sebuah server web yang bisa berjalan di hp android menggunakan node.js
Tool utama untuk membuat server web yang berjalan di hp android adalah termux, apk ini merupakan sebuah aplikasi yang sangat powerfull untuk orang yang ingin belajar tapi terbatas oleh budget untuk membeli sebuah PC ataupun laptop, langsung saja kita masuk tahap pertama,
Install termux
Ada dua platform penyedia aplikasi ini. Yaitu : google playstore dan F-droid, bagi yang ingin versi lebih stabil disarankan untuk menginstall termux melalui F-droid. Setelah menginstall termux, kita perlu menjalankan perintah update, ketikan perintah berikut di terminal termux :
pkg update && pkg upgrade
Setelah itu kita perlu menginstall node.js dengan mengetikan perintah :
pkg install nodejs
node -v npm -v
Buat direktori Proyek
mkdir myserver
cd myserver
Inisialisasi proyek
Didalam direktori proyek, kita perlu menginisialisikan proyek dengan mengetikan :
npm init -y
npm install express
nano server.js
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Halo dari Node.js server di Termux!\n');
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server berjalan di http://localhost:${PORT}`);
});
Jalankan Server dilokal
node server.js
Server berhasil berjalan di http://localhost:3000
Gunakan http-server Untuk File Statis (HTML, CSS, JAVASCRIPT)
npm install -g http-server
nano index.html
<!DOCTYPE html>
<html>
<head>
<title>Halo dari Termux</title>
</head>
<body>
<h1>Server web ini jalan dari HP Android!</h1>
</body>
</html>
npx http-server
Starting up http-server, serving ./
http-server version: 14.1.1
http-server settings:
CORS: disabled
Cache: 3600 seconds
Connection Timeout: 120 seconds
Directory Listings: visible
AutoIndex: visible
Serve GZIP Files: false
Serve Brotli Files: false
Default File Extension: none
Available on:
http://127.0.0.1:8080
http://192.168.1.10:8080
Hit CTRL-C to stop the server
0 Response to "Menjalankan Server Web NodeJs Di Termux"
Post a Comment