A robust RESTful API for managing events, built with Node.js and Express.js. This backend provides endpoints for user authentication, event creation, management, and user registrations for events.
- User Authentication: Secure signup and login with JWT tokens
- Event Management: Create, read, update, and delete events
- User Registration: Users can register for events
- Image Uploads: Support for uploading event images
- Validation: Input validation using express-validator
- Database: SQLite database for data persistence
- Backend: Node.js, Express.js
- Database: SQLite (better-sqlite3)
- Authentication: JWT (jsonwebtoken), bcryptjs for password hashing
- File Uploads: Multer
- Validation: express-validator
- CORS: cors middleware
- Environment: dotenv for configuration
The package.json file is the heart of any Node.js project. It contains metadata about the project, including:
- Project Information: Name, version, description, author, and license details.
- Dependencies: Lists all the npm packages required for the project to run, such as:
express: Web framework for Node.jsbetter-sqlite3: SQLite database driverbcryptjs: Password hashing utilityjsonwebtoken: JWT token generation and verificationmulter: Middleware for handling file uploadsexpress-validator: Input validation middlewarecors: Cross-origin resource sharing middlewaredotenv: Environment variable management
- Scripts: Defines npm scripts for common tasks, e.g.,
"dev":"node --watch app.js"to start the development server with auto-reload on file changes.
Note: This file is gitignored in the repository, so it won't be included in version control. To recreate it, run npm init and install the dependencies listed above.
-
Clone the repository:
git clone <repository-url> cd express-rest-api
-
Install dependencies:
npm install
This will install all required packages including:
- express
- better-sqlite3
- bcryptjs
- jsonwebtoken
- multer
- express-validator
- cors
- dotenv
The application is written in JavaScript and does not require a build step. All source files are ready to run.
However, ensure the database is initialized by running the server, which automatically creates the necessary tables.
The application uses environment variables for configuration management. These variables are loaded from a .env file in the root directory using the dotenv package.
-
Create a
.envfile in the root directory of the project:touch .env
-
Add the following environment variables to your
.envfile:# Server Configuration HOST=localhost PORT=3000 # Environment NODE_ENV=development # JWT Secret (REQUIRED - Generate using methods below) JWT_SECRET=your_generated_secret_here
| Variable | Description | Default | Required |
|---|---|---|---|
HOST |
Host where the application will run | localhost |
No |
PORT |
Port where the application will run | 3000 |
No |
NODE_ENV |
Application environment (development or production) |
development |
No |
JWT_SECRET |
Secret key for JWT token signing and verification | None | Yes |
The JWT_SECRET must be a cryptographically strong random string. Here are several secure methods to generate it:
node -e "console.log(require('crypto').randomBytes(64).toString('hex'))"openssl rand -hex 64head -c 64 /dev/urandom | xxd -p -c 64For development purposes, you can use an online secret generator, but never use this method for production as it compromises security.
Example generated secret:
JWT_SECRET="0ab863ab944fb0e431fc0ccc73c683a40373b966c27323b761f60c4be379602e8aec4f0848de851a651da9797fd9ef6cfc32fccaf1fbd69c53ed93d6e5d89d8c"
- Never commit .env files - The
.envfile should be added to.gitignore - Use strong secrets - Generate JWT secrets with at least 256 bits (32 bytes) of entropy
- Rotate secrets regularly - Change JWT secrets periodically in production
- Use different secrets per environment - Development and production should have different secrets
- Restrict file permissions - Set
.envfile permissions to600(owner read/write only)
-
After setting up your
.envfile with all required variables, start the server:npm run dev
-
The server will start on the specified port and initialize the database automatically.
The application will log the port it's running on (e.g., "Server is running on port 3000").
POST /users/signup- User registrationPOST /users/login- User login
GET /users/events- Get all events (Note: Route might be under /user as per app.js)- Additional event routes (check routes/events.js if implemented)
/images/*- Serve uploaded images from public/images
- Users: id, name, email, password, created_at
- Events: id, title, description, address, date, image, created_by, created_at
- Registrations: id, event_id, user_id, created_at
- Environment variables are loaded from
.envfile - Database file:
database.db - Uploaded images:
public/images/
This project is licensed under the Apache 2.0 License.