Frameworks express សម្រាប់ប្រើប្រាស់ក្នុងការងាយស្រួលក្នុងការបង្កើត project។ នេះជាវែបសាយ officail express។
មុនដំបូងយើង file មួយដោយឈ្មោះថា server.js នៅក្នុង folder របស់យើង
បន្ទាប់មកយើង install package
npm init

យើង install express
npm install express

បើក localhost
បន្ទាប់ពីយើង install វារួចរាលហើយមកប្រើវាម្តង ដោយយើងសរសេរកូដនៅក្នុង file server.js របស់យើង
const express = require('express'); // Create core module const app = express(); app.listen(3000);

យើងបានបើក port របស់វាហើយ ក៏ប៉ុន្តែយើងពិបាកដឹងអំពីលេខ port របស់ប៉ុន្តែនៅពេលយើង run ដោយយើងបន្ថែម console.log ប្រាប់អំពី port នៅពេល run
const express = require('express'); // Create core module const app = express(); app.listen(3000, ()=>{ console.log("Server started on port 3000") });

យើងបើក browser នៅ localhost របស់យើង
http://localhost:3000/

route
const express = require('express'); // Create core module const app = express(); app.get('/', (req,res)=>{ // MARK: '/' home route res.send("<h1>Hello Ros Dul!</h1>") }) app.listen(3000, ()=>{ console.log("Server started on port 3000") });

Add route
យើងបាន add route about
const express = require('express'); // Create core module const app = express(); app.get('/', (req,res)=>{ // MARK: '/' home route res.send("<h1>Hello Ros Dul!</h1>"); }) app.get('/about',(req,res)=>{ res.send("<h4>I'm Ros Dul and studying node js!</h4>") }) app.listen(3000, ()=>{ console.log("Server started on port 3000"); });

0 Comments