Skip to content

Commit cd51513

Browse files
committed
first commit
0 parents  commit cd51513

File tree

12 files changed

+1645
-0
lines changed

12 files changed

+1645
-0
lines changed

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Node modules
2+
node_modules/
3+
4+
# Environment variables
5+
.env
6+
7+
# Logs
8+
logs
9+
*.log
10+
npm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
14+
# OS files
15+
.DS_Store
16+
Thumbs.db
17+
18+
# IDE files
19+
.vscode/
20+
.idea/
21+
22+
# MongoDB data directory (if any local data storage)
23+
data/
24+
25+
# Optional npm cache directory
26+
.npm
27+
28+
# Optional REPL history
29+
.node_repl_history

README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Notes API
2+
3+
A RESTful API for managing personal notes with user authentication. Built with Node.js, Express, and MongoDB.
4+
5+
## Features
6+
7+
- User registration and authentication
8+
- Create, read, update, and delete notes
9+
- Search notes by title
10+
- Filter notes by tags
11+
- Pagination for note listings
12+
- Archive and pin notes
13+
- Secure routes with authentication middleware
14+
15+
## Installation
16+
17+
1. Clone the repository:
18+
```
19+
git clone <repository-url>
20+
cd notes-api
21+
```
22+
23+
2. Install dependencies:
24+
```
25+
npm install
26+
```
27+
28+
3. Set up environment variables:
29+
Create a `.env` file in the root directory with the following:
30+
```
31+
SECRET=your-secret-key
32+
PORT=3000
33+
```
34+
35+
4. Start MongoDB locally or update the connection string in `index.js` if using a remote database.
36+
37+
5. Run the application:
38+
```
39+
node index.js
40+
```
41+
42+
The server will start on port 3000 (or the port specified in `.env`).
43+
44+
## Usage
45+
46+
The API provides endpoints for user authentication and note management. All note-related endpoints require authentication except registration and login.
47+
48+
### Authentication
49+
50+
- Register a new user with POST `/register`
51+
- Login with POST `/login`
52+
- Logout with POST `/logout`
53+
- Get user profile with GET `/profile`
54+
55+
### Notes
56+
57+
- Create a note: POST `/notes`
58+
- Get all notes: GET `/notes` (supports query parameters for search, tag, page, limit)
59+
- Get a specific note: GET `/notes/:id`
60+
- Update a note: PUT `/notes/:id`
61+
- Delete a note: DELETE `/notes/:id`
62+
63+
## API Endpoints
64+
65+
### Authentication Routes
66+
67+
| Method | Endpoint | Description | Auth Required |
68+
|--------|--------------|--------------------------|---------------|
69+
| POST | /register | Register a new user | No |
70+
| POST | /login | Login user | No |
71+
| POST | /logout | Logout user | Yes |
72+
| GET | /profile | Get user profile | Yes |
73+
74+
### Notes Routes
75+
76+
| Method | Endpoint | Description | Auth Required |
77+
|--------|--------------|--------------------------|---------------|
78+
| POST | /notes | Create a new note | Yes |
79+
| GET | /notes | Get all user notes | Yes |
80+
| GET | /notes/:id | Get a specific note | Yes |
81+
| PUT | /notes/:id | Update a note | Yes |
82+
| DELETE | /notes/:id | Delete a note | Yes |
83+
84+
Query parameters for GET /notes:
85+
- `q`: Search term for title (case-insensitive)
86+
- `tag`: Filter by tag
87+
- `page`: Page number for pagination (default 1)
88+
- `limit`: Number of notes per page (default 10)
89+
90+
## Models
91+
92+
### User
93+
- `email`: String (required, unique)
94+
- `username`: String (handled by passport-local-mongoose)
95+
- `password`: String (hashed by passport-local-mongoose)
96+
97+
### Note
98+
- `title`: String (required, max 50 characters)
99+
- `content`: String (required, min 3 characters)
100+
- `tags`: Array of Strings (required)
101+
- `user`: ObjectId (reference to User)
102+
- `isArchived`: Boolean (default false)
103+
- `isPinned`: Boolean (default false)
104+
- `createdAt`: Date
105+
- `updatedAt`: Date
106+
107+
## Technologies Used
108+
109+
- Node.js
110+
- Express.js
111+
- MongoDB
112+
- Mongoose
113+
- Passport.js (with Local Strategy)
114+
- Express Session
115+
116+
## Contributing
117+
118+
1. Fork the repository
119+
2. Create a feature branch
120+
3. Make your changes
121+
4. Submit a pull request
122+
123+
## License
124+
125+
This project is licensed under the ISC License.

index.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const dotenv = require('dotenv');
2+
dotenv.config();
3+
4+
const mongoose = require('mongoose');
5+
const express = require('express');
6+
const app = express();
7+
8+
const session = require('express-session');
9+
const passport = require('passport');
10+
const LocalStrategy = require('passport-local');
11+
const User = require('./models/user');
12+
13+
app.use(session({
14+
secret: process.env.SECRET || 'verybadsecret',
15+
resave: false,
16+
saveUninitialized: false
17+
}));
18+
19+
// Passport initialization
20+
app.use(passport.initialize());
21+
app.use(passport.session());
22+
23+
// Strategy setup
24+
passport.use(new LocalStrategy(User.authenticate()));
25+
passport.serializeUser(User.serializeUser());
26+
passport.deserializeUser(User.deserializeUser());
27+
28+
29+
const notesRoutes = require('./routes/notes');
30+
const authRoutes = require('./routes/auth')
31+
32+
mongoose.connect('mongodb://127.0.0.1:27017/notes_API')
33+
.then(() => {
34+
console.log("Mongo Connection Open")
35+
}).catch((err) => {
36+
console.log("Error", err)
37+
});
38+
39+
app.use(express.json());
40+
41+
app.use('/notes', notesRoutes);
42+
app.use('/', authRoutes);
43+
44+
app.get('/', (req, res)=>{
45+
res.status(200).json({msg : 'The HomePage'})
46+
});
47+
48+
app.use((err, req, res, next) => {
49+
const { statusCode = 500, message = "Internal Server Error" } = err;
50+
res.status(statusCode).json({ error: message });
51+
});
52+
53+
const PORT = process.env.PORT || 3000
54+
55+
app.listen(PORT, ()=>{
56+
console.log(`App is listening on port ${PORT}`)
57+
});

middleware.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports.isLoggedIn = function(req, res, next){
2+
if(!req.isAuthenticated()){
3+
return res.status(401).json({msg : 'You must be logged in!'});
4+
}
5+
next();
6+
};

models/notes.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const mongoose = require('mongoose');
2+
const { Schema } = mongoose;
3+
4+
const noteSchema = new Schema({
5+
title : {
6+
type : String,
7+
required : true,
8+
trim : true,
9+
maxlength : 50
10+
},
11+
content : {
12+
type : String,
13+
required : true,
14+
minlength : 3
15+
},
16+
tags :[
17+
{
18+
type :String,
19+
required : true
20+
}
21+
],
22+
user : {
23+
type : Schema.Types.ObjectId,
24+
ref : 'User',
25+
required : true
26+
},
27+
isArchived: {
28+
type: Boolean,
29+
default: false,
30+
},
31+
isPinned: {
32+
type: Boolean,
33+
default: false,
34+
},
35+
},
36+
{ timestamps: true }
37+
);
38+
39+
module.exports = mongoose.model('Note', noteSchema);

models/user.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const mongoose = require('mongoose');
2+
const passportLocalMongoose = require('passport-local-mongoose');
3+
const { Schema } = mongoose;
4+
5+
const userSchema = new Schema({
6+
email : {
7+
type : String,
8+
required : true,
9+
unique : true
10+
}
11+
});
12+
13+
userSchema.plugin(passportLocalMongoose);
14+
15+
module.exports = mongoose.model('User', userSchema);

0 commit comments

Comments
 (0)