គឺជាការប្រើប្រាស់ files ឬ function នៅក្នុង main file របស់ node js
មុនដំបូងយើងបង្កើត file មួយសម្រាប់ដាក់ function ឬដាក់កូដផ្សេងៗ។ ដោយយើងសូមដាក់ឈ្មោះថា myFun.js
myFun.js
function testOne(){ console.log("I study Node js bassic!") } const v = "Test create Variable" //Export file for using in index.js module.exports.testOne = testOne module.exports.v = v

index.js
const http = require('node:http');// This core module http // Create Module for import myFun.js const mod = require("./myFun")// Part of file const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end(mod.v); //Call Variable from file myFun.js }); server.listen(port, hostname, () => { mod.testOne()// Call function from file myFun.js });
ពេលយើង run វា
node index.js

0 Comments