This is a starter project for an Express API using Typescript and MongoDB.
- Open a terminal and clone the repo:
git clone https://github.com/Marj4n/start-express-ts.git [your-project-name]
- Go to the project directory and install dependencies:
cd start-express-ts
# using yarn
yarn install
# using npm
npm install
- Setup environment variables:
cp .env.example .env
Edit the .env
file and add your MongoDB connection string, port and secret. for example:
PORT=8080
MONGODB_URI=mongodb://localhost:27017/express-ts-api
SECRET=secret
- Start the server:
# using yarn
yarn start
# using npm
npm start
The API url is https://start-express-ts-marj4n.vercel.app/api
and the following endpoints are available:
- POST
/auth/register
- Register a new user.
Required fields: username
, email
, password
Example request body:
{
"email": "jokowi@gmail.com",
"password": "jokowi12345",
"username": "jokowi"
}
- POST
/auth/login
- Login a user.
Required fields: email
, password
Example request body:
{
"email": "jokowi@gmail.com",
"password": "jokowi12345"
}
- GET
/auth/logout
- Logout a user.
Description: Only authenticated users can logout.
- GET
/users
- Get all users.
Description: Only authenticated users can get all users.
- GET
/users/:id
- Get a user by id.
Description: Only Authenticated users can get a user by id.
Url example: http://localhost:8080/api/users/2
- PATCH
/users/:id
- Update a user by id.
Description: Only the user's owner can update the user.
Url example: http://localhost:8080/api/users/2
Required fields: username
Example request body:
{
"username": "Jamashin"
}
- DELETE
/users/:id
- Delete a user by id.
Description: Only the user's owner can delete the user.
Url example: http://localhost:8080/api/users/2
- GET
/books
- Get all books.
Description: Everyone can get all books.
- GET
/books/:id
- Get a book by id.
Description: Everyone can get a book by id.
Url example: http://localhost:8080/api/books/2
- POST
/books
- Create a new book.
Description: Only authenticated users can create a book.
Required fields: title
, author
, description
, publicationYear
Example request body:
{
"title": "Dangerous Zombie",
"author": "Genmu",
"description": "Gachan! Level Up! D-Dangerous! Zombie! D-Dangerous! Zombie!",
"publicationYear": 2016
}
- PATCH
/books/:id
- Update a book by id.
Description: Only the book's owner can update the book.
Url example: http://localhost:8080/api/books/2
Required fields: title
, author
, description
, publicationYear
Example request body:
{
"title": "Taddle Legacy",
"author": "Genmu",
"description": "Gachan! Level Up! Tadoru rekishi! Mezameru kishi! Taddle Legacy~!",
"publicationYear": 2017
}
- DELETE
/books/:id
- Delete a book by id.
Description: Only the book's owner can delete the book.
Url example: http://localhost:8080/api/books/2