การติดตั้ง Node.js, Express.js และ TypeScript
Sirichai Teerapattarasakul / January 22, 2023
4 min read
วิธีการการติดตั้ง Node.js โดยติดตั้งร่วมกับ ExpressJS (Framework ยอดนิยม) และ TypeScript รวมถึง Libraries อื่นๆ ที่จำเป็นสำหรับเริ่มต้น Project ตามรายละเอียดด้านล่างนี้
สร้าง package.json
npm init -y
ติดตั้ง Express
npm install --save express
ติดตั้ง Typescript
- tsc = สำหรับ Compile Typescript แปลงเป็น Javascript (ต้องสั่งรันเองภายหลัง)
- ts-node = Compile Typescript และรันให้ทันที
- ts-node-dev = Compile Typescript และรันให้ทันที โดยมีการ Process ค้างไว้สำหรับ Dev รวมถึงจะ Restart อัตโนมัติเมื่อมีการแก้ไขไฟล์
- @types/express = กำหนด Type สำหรับ Express.js
- @types/node = กำหนด Type สำหรับ Node.js
npm install --save-dev tsc typescript ts-node ts-node-dev @types/express @types/node
สร้าง tsconfig.json
tsc --init
ปรับแต่ง tsconfig.json
- rootDir = เก็บ Sourecode
- outDir = compile ไปที่
{
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist"
}
}
เพิ่ม script ใน package.json
เพื่อเรียกใช้ npm run dev สำหรับรันระบบ
"scripts": {
"dev": "ts-node-dev src/server.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
ติดตั้ง Sequelize Object Relational Mapping (ORM)
รองรับการจัดการฐานข้อมูล เช่น Postgres, MySQL, MariaDB, SQLite และ Microsoft SQL Server ใช้ตัวไหนเลือกติดตั้งตัวนั้น
npm install --save sequelize
npm install --save pg pg-hstore # Postgres
npm install --save mysql2 #MySQL
npm install --save mariadb #MariaDB
npm install --save sqlite3 #SQLite
npm install --save tedious # Microsoft SQL Server
npm install --save oracledb # Oracle Database
ตัวอย่างโครงสร้าง
my-api/
|--public/
| |--css/
| |--js/
| |--images/
| |--uploads/
| | |--content/
| | | |--yyyymm/
| | |--blog/
| | | |--yyyymm/
| | |--avatar/
| | | |--yyyymm/
|--src/
| |--configs/
| | |--appConfig.ts
| | |--authConfig.ts
| | |--databaseConfig.ts
| | |--cacheConfig.ts
| | |--mailConfig.ts
| | |--sessionConfig.ts
| | |--serviceConfig.ts
| | |--debugConfig.ts
| | |--passportConfig.ts
| |--controllers/
| | |--userController.ts
| | |--memberController.ts
| | |--cartController.ts
| | |--orderController.ts
| | |--admin/
| | | |--dashboardController.ts
| | | |--userController.ts
| | | |--memberController.ts
| | | |--orderController.ts
| | | |--reportController.ts
| |--models/
| | |--userModel.ts
| | |--orderModel.ts
| | |--memberModel.ts
| |--exports/
| | |--userExport.ts
| | |--memberExport.ts
| | |--orderExport.ts
| |--routes/
| | |--webRoute.ts
| | |--apiRoute.ts
| |--middlewares/
| | |--authMiddleware.ts
| | |--logMiddleware.ts
| |--providers/
| | |--appProvider.ts
| | |--authProvider.ts
| |--resources/
| | |--views/
| | | |--admin/
| | | | |--dashboard.ejs
| | | | |--profile.ejs
| | | | |--layouts/
| | | | | |--main.ejs
| | | | | |--plain.ejs
| | | | |--partials/
| | | | | |--header.ejs
| | | | | |--footer.ejs
| | | | | |--sidebar.ejs
| | | | | |--nav.ejs
| | | |--web/
| | | | |--index.ejs
| | | | |--page.ejs
| | | | |--layouts/
| | | | | |--main.ejs
| | | | | |--plain.ejs
| | | | |--partials/
| | | | | |--header.ejs
| | | | | |--footer.ejs
| | | | | |--sidebar.ejs
| | | | | |--nav.ejs
| | | |--errors/
| | | | |--404.ejs
| | | | |--403.ejs
| | | | |--500.ejs
| | |--lang/
| | | |--en/
| | | | |--validation.ts
| | | | |--pagination.ts
| | | | |--passwords.ts
| | | |--th/
| | | | |--validation.ts
| | | | |--pagination.ts
| | | | |--passwords.ts
| |--app.ts
| |--tests/
| | |--unit/
| | | |--ExampleTest/
|--tsconfig.json
|--package.json
|--.env
publicเก็บ static file ทั้งหมด เช่น css, รูป, ไฟล์ต่างๆsrcเก็บ source code ทั้งหมดconfigเก็บ configuration เช่น config.tscontrollersเก็บส่วน handle logic ของแต่ละ routesmodelsเก็บส่วนที่ทำหน้าที่แทน data modelsexportsเก็บส่วนที่ทำหน้าที่แทน export เช่น ส่งออกเป็น excelroutesเก็บส่วนการกำหนดเส้นทาง routes ทั้งหมดmiddlewaresเก็บ middlewaresprovidersเก็บส่วน service กลางหรือประมวลผลเป็น global ในทุกๆ endpoint หรือ pageresourcesเก็บส่วนแสดงผลเช่น template (pug), ภาษาtestsเก็บส่วนสำหรับทดสอบระบบเช่น Unit Testapp.tsเป็นไฟล์เริ่มต้น entry point ของ Express.js server, ติดต่อ database และ import/use routes และ middlewarestsconfig.jsonเก็บ TypeScript configurationpackage.jsonเก็บ information ของ project รวมถึง dependencies.envเก็บ environment variables