A simple RESTful API for a blog platform built with Node.js, Express, and MongoDB. It supports user authentication, post creation, image uploads, and tag management.
- User registration and login with JWT authentication
- Create, read, update, and delete blog posts
- Upload images for posts
- Tag management for posts
- Input validation and error handling
- Node.js
- Express
- MongoDB & Mongoose
- JWT (jsonwebtoken)
- Multer (file uploads)
- bcrypt (password hashing)
- express-validator (input validation)
- CORS
- Clone the repository:
git clone <repo-url> cd NodeJS-Project-main
- Install dependencies:
npm install
- Set up MongoDB:
- Update the MongoDB connection string in
index.jsif needed.
- Update the MongoDB connection string in
- Start the server:
For development with auto-reload:
npm start
npm run start:dev
POST /auth/register— Register a new userPOST /auth/login— Login and receive JWT tokenGET /auth/me— Get current user info (requires auth)
GET /posts— Get all postsGET /posts/:id— Get a single post by IDPOST /posts— Create a new post (requires auth)PATCH /posts/:id— Update a post (requires auth)DELETE /posts/:id— Delete a post (requires auth)
GET /tags— Get last tagsGET /posts/tags— Get last tags
POST /upload— Upload an image (requires auth)GET /uploads/:filename— Access uploaded images
index.js # Main server file
package.json # Project metadata and dependencies
validations.js # Input validation rules
controllers/ # Route controllers
index.js
PostController.js
UserController.js
models/ # Mongoose models
Post.js
User.js
uploads/ # Uploaded images
utils/ # Utility functions
checkAuth.js
handleValidationErrors.js
index.js
- All protected routes require a valid JWT token in the
Authorizationheader. - Images are stored in the
uploads/directory and served statically. - Input validation is handled using
express-validator.
ISC