🌐
Miniclass Web
  • Miniclass Web
  • 1. Pengenalan Pemrograman Web
    • Cara Kerja Website
    • Pengenalan Backend dan Frontend
    • HTTP Request dan HTTP Response
  • 2. HTML
    • Pengenalan HTML
    • Text Formatting
    • Link
    • Media
    • List
    • Table
    • Form
    • Grouping
  • 3. CSS
    • Pengenalan CSS
    • Selector CSS
    • Text Formatting
    • Background
    • Border
    • Height dan Width
    • Spacing
    • Layouting
    • Responsive Design
    • Membuat Template Web Responsive
  • 4. Javascript
    • Pengenalan JavaScript
    • Javascript for Java developer
    • Array
    • Fungsi
    • Manipulasi DOM
    • Event
    • Fetch API
    • Object JavaScript
    • UI Component Lanjutan
  • 5. Node.js
    • Pengenalan Node.js
    • Membuat Server HTTP
    • Routing
    • Import Export
    • Node Package Manager
    • Nodemon
  • 6. Express.js
    • Pengenalan Express.js
    • HTTP Method
    • Menerima Data dari URL
    • Middleware
    • Menerima Data dari Body
    • Mengunggah File
    • Menampilkan File dalam Folder
    • Menyimpan Data ke Database MySQL
  • 7. Modern Frontend Development
    • ECMAScript
    • Transpiler
    • Module Bundler
  • 8. React Fundamental
    • Pengenalan React JS
    • Props
    • Event
    • State
    • Conditional Rendering
    • Component Mapping
    • Side Effect
    • Custom Hook
  • 9. React State Management
    • Lifting State Up
    • Context
    • Reducer
  • 10. React Performance
    • Debounce
    • Throttling
    • Memoization
    • Profiling
  • 11. React Testing
    • Component Testing
    • Hook Testing
    • Mocking
    • End to End Testing
  • 12. Object Relational Mapping
    • Pengenalan Object Relational Mapping
  • 13. Document Oriented Database
    • Document Oriented Database
    • Object Document Mapping
  • 14. Web Service
    • REST
    • GraphQL
  • 15. Autentikasi
    • JSON Web Token
    • Membuat Web Service Sederhana
  • 16. Microservices
    • Microservices
    • Microservices Gateway
    • Message Queue
    • Remote Procedure Call
    • Membuat Micro Service Sederhana
  • 18. Caching
Powered by GitBook
On this page
  • 1. Permasalahan
  • 2. Export & Import Module
  • 3. Cara Export & Import Module
  • 3.1. Export
  • 3.2. Import
  • 4. Contoh Kasus

Was this helpful?

  1. 5. Node.js

Import Export

1. Permasalahan

Untuk membangun aplikasi yang baik, kita tidak boleh menulis code dalam satu file saja. Kita harus membagi code ke dalam beberapa file sesuai kebutuhan. Sehingga baris code dalam 1 file tidak terlalu banyak dan akhirnya akan lebih mudah dipahami.

2. Export & Import Module

Karena code akan terbagi ke dalam beberapa file maka diperlukan export variabel maupun fungsi. Sedangkan untuk menggunakan variabel atau fungsi yang di export tadi,maka harus melakukan import terlebih dahulu.

3. Cara Export & Import Module

Jika dalam sebuah file terdapat variabel maupun fungsi yang akan digunakan pada file lain, maka variabel atau fungsi tersebut harus diexport terlebih dahulu menggunakan module.exports.

Sedangkan file lain yang ingin menggunakan variable atau fungsi yang diexport tadi, file tersebut harus mengimport terlebih dahulu menggunakan require()

3.1. Export

Semisal terdapat 2 file, file pertama bernama data.js dan file kedua bernama app.js. Dalam skenario ini file data.js akan terdapat variabel yang nantinya akan digunakan oleh file app.js.

Mari kita buat file data.js terlebih dahulu

// data.js

var data = {
  nama: "budi",
  kelas: "TI-4B",
  alamat: "malang",
};

module.exports = data;

3.2. Import

Pada langkah ini kita membuat file app.js yang nantinya akan menggunakan data yang berasal dari file data.js

// app.js

var data = require("./data.js");

console.log(data.nama); // budi
console.log(data.kelas); // TI-4B
console.log(data.alamat); // malang

4. Contoh Kasus

Sebelumnya kita telah membuat web server dengan beberapa konfigurasi routing seperti berikut.

// index.js
var http = require("http");

var contacts = [
  { name: "budi", phone: "085739582738" },
  { name: "imam", phone: "085637562615" },
  { name: "siti", phone: "085462736253" },
];

http
  .createServer(function (req, res) {
    if (req.url == "/") {
      res.write("Welcome");
    } else if (req.url == "/about") {
      res.write("This web service contains contacts data");
    } else if (req.url == "/contact") {
      res.write(JSON.stringify(contacts));
    } else {
      res.write("Page not found");
    }
    res.end();
  })
  .listen(8080);

Di dalam file index.js ini terdapat sebuah variable contacts yang berisi data - data kontak. Untuk membuat file index.js ini lebih rapi, maka kita harus memindahkan variable contacts ke dalam file baru yang akan dinamakan contacts.js.

//contacts.js

var contacts = [
  { name: "budi", phone: "085739582738" },
  { name: "imam", phone: "085637562615" },
  { name: "siti", phone: "085462736253" },
];

module.exports = contacts;

lalu pada index.js kita dapat merubahnya menjadi seperti berikut.

// index.js
var http = require("http");
var contacts = require("./contacts.js");

http
  .createServer(function (req, res) {
    if (req.url == "/") {
      res.write("Welcome");
    } else if (req.url == "/about") {
      res.write("This web service contains contacts data");
    } else if (req.url == "/contact") {
      res.write(JSON.stringify(contacts));
    } else {
      res.write("Page not found");
    }
    res.end();
  })
  .listen(8080);

Pada materi kali ini, kita tidak mengubah aplikasi web server kita sama sekali, namun yang kita lakukan adalah mengorganisasikan code yang kita ketik menjadi ke beberapa file.

Sehingga tiap file akan memiliki fungsinya sendiri - sendiri, dalam hal ini contacts.js berfungsi untuk menyimpan data (variabel contacts). Sedangkan index.js akan fokus ke code web server yang kita buat.

Dengan mengorganisasikan code kita ke beberapa file dengan fungsinya masing - masing, maka code yang kita buat akan lebih mudah dipahami

PreviousRoutingNextNode Package Manager

Last updated 4 years ago

Was this helpful?