Skip to content

lihasahil/BACKEND_TYPESCRIPT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Express + JWT Authentication (TypeScript)

A boilerplate project for building a REST API with Express, TypeScript, and JWT authentication.

Features

  • 🚀 Express.js for building REST APIs\
  • 🔐 JWT (JSON Web Tokens) for authentication & authorization\
  • 📝 TypeScript with type safety\
  • ⚡ Middleware for request validation\
  • 🗂️ Project structure for scalability

Getting Started

1. Clone the repo

git clone https://github.com/your-username/express-jwt-typescript.git
cd express-jwt-typescript

2. Install dependencies

npm install

3. Setup environment variables

Create a .env file in the root of the project:

PORT=5000
JWT_SECRET=your_secret_key
TOKEN_EXPIRES_IN=1h

4. Run the development server

npm run dev

5. Build for production

npm run build
npm start

Project Structure

src/
├── controllers/      # Route handlers
├── middlewares/      # Auth & error middlewares
├── routes/           # API routes
├── services/         # Business logic
├── types/            # TypeScript types
├── utils/            # Helper functions
├── index.ts          # App entry point

Example Routes

Register a User

POST /api/auth/register
Content-Type: application/json

{
  "username": "testuser",
  "password": "password123"
}

Login

POST /api/auth/login
Content-Type: application/json

{
  "username": "testuser",
  "password": "password123"
}

Response:

{
  "token": "your_jwt_token"
}

Protected Route

GET /api/profile
Authorization: Bearer <token>

Scripts

  • npm run dev → Run in dev mode with nodemon & ts-node\
  • npm run build → Build TypeScript into JavaScript (dist/)\
  • npm start → Run compiled JavaScript in production

Tech Stack


License

MIT © Sahil Shrestha

About

Practice of Express using Typescript

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors