1.5. Cài đặt và triển khai ứng dụng với express

1. Cài đặt Express.js

  • Cài đặt Express qua npm:

npm install express --save

2. Tạo file app.js và sử dụng Express

  • Tạo file mới app.js

touch app.js
  • Thêm mã để tạo server với Express

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

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

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});

3. Chạy ứng dụng

  • Chạy server bằng Node.js:

node app.js

Mở trình duyệt và truy cập vào địa chỉ http://localhost:3000. Bạn sẽ thấy dòng chữ "Hello Express!" xuất hiện trên màn hình.

Last updated