This course is based on this template
akkaraponph / nodejs-ts-fastify-mvc-starter-template
This repo node api crud template - folder-by-type
Node.js TypeScript Fastify MVC Starter Template
A production-ready starter template for building RESTful APIs with Node.js, TypeScript and Fastify. This template follows the MVC (Model-Routes-Controllers-Services) architecture pattern, providing a clean and scalable foundation for your backend projects.
🚀 Features
Fastify - Fast and low overhead web framework
TypeScript - Type-safe development
Sequelize ORM - Database abstraction with PostgreSQL/MySQL support
JWT Authentication - Secure token-based authentication
Swagger Documentation - Auto-generated API documentation
CORS Support - Cross-origin resource sharing configured
Error Handling - Custom error handling middleware
MVC Architecture - Clean separation of concerns
Environment Configuration - Multi-environment support (dev, test, production)
📁 Project Structure
├── src/
│ ├── app.ts # Fastify app configuration
│ ├── config/ # Configuration files
│ │ ├── config.ts # Environment variables
│ │ ├── db.config.ts # Database configuration
│ │ └── swagger/ # Swagger documentation setup
│ ├── controllers/ # Request handlers (business logic)
│ ├──…
Flag
This YouTube tutorial is in Thai. I’m keeping this post in English so everyone can quickly understand the main concepts alongside the video. For deeper understanding, feel free to ask ChatGPT or your favorite AI.
What you will learn
This course teaches you how to build an API using:
Node.js
Fastify (a web framework)
Sequelize (for working with databases)
MySQL (database)
About MRCS
MRCS means Model-Routes-Controllers-Services. This is a way to organize your code. It helps you keep your code clean and easy to understand.
BillowDev Youtube
Setup Videos
Watch these videos first to install the tools you need:
How to Install Git and using GitHub
How to Install Node.js and VS code
How to Install Laragon
Node js TypeScript Fastify EP 01 Intro
In this episode, you will learn how to start your project and create your first API.
Step 1 - Start a new project
First, create a new Node.js project. Open your terminal and type:
npm init -yThis command creates a file called package.json. This file stores information about your project.
Step 2 - Using Yarn (Optional)
You can use npm or yarn. Both are package managers. They help you install libraries for your project.
If you want to use yarn, install it first:
npm install -g yarnHere are the yarn commands you need to know:
npm installis the same asyarn addnpm install -Dis the same asyarn add -Dnpm runis the same asyarn
Step 3 - Install TypeScript tools
You need to install TypeScript and some tools. TypeScript is a language that adds types to JavaScript.
Type this command in your terminal:
npm install --save-dev ts-node @types/node typescriptOr if you use yarn:
yarn add -D ts-node @types/node typescriptStep 4 - Create TypeScript config file
Now create a TypeScript configuration file. This file tells TypeScript how to work with your code.
Type this command:
npx tsc --initThis creates a file called tsconfig.json. Copy this code into that file:
tsconfig.json
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
/* Language and Environment */
"target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
"baseUrl": "./src", /* Specify the base directory to resolve non-relative module names. */
"outDir": "./dist", /* Specify an output folder for all emitted files. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": [
"**/*.ts"
, "src/config/db.config.ts" ],
"exclude": [
"node_modules",
"**/*.d.ts",
"**/*.spec.ts"
]
}Step 5 - Install Fastify
Fastify is a web framework. It helps you build APIs quickly.
Install it with this command:
npm install fastifyOr if you use yarn:
yarn add fastifyStep 6 - Create your first file
Create a new file called index.ts in your project root folder.
Step 7 - Write your first API
Now you will write code to create your first API. Follow these steps:
Step 7.1 - Import Fastify
import fastify from "fastify";This line imports the Fastify library.
Step 7.2 - Create the app
Next, create your app:
const app = fastify()Step 7.3 - Create your first route
A route is a URL that your API can respond to. Create your first route:
app.get("/", async () => "SERVER");This creates a route at "/". When someone visits this URL, they will see "SERVER".
Step 7.4 - Start your server
Now you need to start your server. Add this code:
const PORT = 5000
app.listen({port:Number(PORT)}, (err) => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(`SERVE ON ${PORT}`)
})This code starts your server on port 5000.
Step 7.5 - Run your API
Save your file. Then open your terminal and type:
ts-node index.tsYou should see the message "SERVE ON 5000" in your terminal. This means your API is running!
Step 8 - Make your logs look better
The logs help you see what your API is doing. Let's make them look better.
Step 8.1 - Add logging options
Change your app code to this:
const app = fastify({
logger: true
})This turns on logging.
Step 8.2 - Update your listen code
Replace the code from step 7.4 with this:
const PORT = 5000
app.listen({port:Number(PORT)}, (err) => {
if (err) {
app.log.error(err);
process.exit(1)
}
app.log.info(`SERVE ON ${PORT}`)
})This uses the logger instead of console.log.
Step 8.3 - See your logs
When you run your API now, you will see logs like this:
{"level":30,"time":1676779096246,"pid":25344,"hostname":"billo","msg":"Server listening at http://[::1]:5000"}
{"level":30,"time":1676779096248,"pid":25344,"hostname":"billo","msg":"Server listening at http://127.0.0.1:5000"}
{"level":30,"time":1676779096248,"pid":25344,"hostname":"billo","msg":"SERVE ON 5000"}Step 9 - Test your API
You can test your API with Thunder Client. It is a tool for testing APIs. You can find it in the Visual Studio Code extensions.
You can also use other tools like Postman or Insomnia. These tools let you test different HTTP methods like GET, POST, PUT, PATCH and DELETE. If your API uses GET method, you can also test it in your browser.
Originally published by me at the dev to - Akkarapon Phikulsri on DEV Community.



