This repository contains an Express.js API for managing notes with user authentication. It provides endpoints for user sign-up, sign-in, and CRUD operations on notes.
- User authentication: Sign-up and sign-in functionality.
- Secure storage: User passwords are hashed before storage.
- CRUD operations: Create, read, update, and delete notes.
- Authorization: Ensure users can only access their own notes.
-
Clone the repository to your local machine:
git clone https://github.com/notrealmaurya/NotesApi-expressJS.git
-
Navigate to the project directory:
cd NotesApi-expressJS
-
Install dependencies:
npm install
-
Configure environment variables:
- Create a
.env
file in the root directory. - Define the following variables:
PORT
: Port for the Express server.MONGO_URL
: MongoDB connection URL.- Optionally, any other configuration variables.
- Create a
-
Start the server:
npm start
- Sign-up: Register a new user by sending a POST request to
/users/signup
with the user's username, email, and password. - Sign-in: Log in as an existing user by sending a POST request to
/users/signin
with the user's email and password. This will return an authentication token.
- Create a Note: Send a POST request to
/notes
with the note content in the request body. Include the user's authentication token in the request headers for authorization. - Read Notes: Retrieve all notes by sending a GET request to
/notes
. Include the user's authentication token in the request headers. - Update a Note: Send a PUT request to
/notes/:id
with the note ID in the URL and the updated content in the request body. Include the user's authentication token in the request headers for authorization. - Delete a Note: Send a DELETE request to
/notes/:id
with the note ID in the URL. Include the user's authentication token in the request headers for authorization.
- Express.js: Web application framework for Node.js.
- MongoDB: NoSQL database for storing notes and user data.
- Mongoose: Object Data Modeling (ODM) library for MongoDB and Node.js.
- JWT (JSON Web Tokens): Token-based authentication mechanism.
const mongoose = require('mongoose');
const UserSchema = mongoose.Schema({
username: {
type: String,
required: true
},
password: {
type: String,
required: true
},
email: {
type: String,
required: true
}
}, { timestamps: true });
module.exports = mongoose.model('User', UserSchema);
const mongoose = require('mongoose');
const NoteSchema = mongoose.Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
}
}, { timestamps: true });
module.exports = mongoose.model('Note', NoteSchema);
Contributions to this project are welcome! If you encounter any bugs, have suggestions for improvements, or want to contribute new features, please open an issue or submit a pull request.