This document might be outdated relative to the documentation in English. For the latest updates, please refer to the documentation in English.

âś–

Hello world

Bu yerda Express dasturga eng sodda misol keltirilgan.


const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

req (request) va res (response) Node taqdim etayotgan obyektlar hisoblanadi, shuning uchun req.pipe(), req.on('data', callback) Express talab qilmaydigan boshqa obyektlar.

Dastur serverni ishga tushuradi va 3000 portdagi aloqani eshitib turadi. Javob sifatida bosh sahifaga “Hello World!” so’zi jo’natiladi. Qolgan barcha sahifalarga esa 404 Not Found javobi jo’natiladi.

Kodni yozib, app.js faylida saqlab qoying. Uni ishga tushurish uchun quyidagi buyruqni ishga tushiring:

$ node app.js

Natijani ko’rish uchun, brauzerdan http://localhost:3000/ manziliga kiring.